2014-07-04 51 views
0

我有3個數據庫表。 1有細節,1有功能,1有小時。該功能和小時表具有來自詳細信息表的ID和ID。我想要做的是創建1個數組,其中有3個數組,其中一個用於細節,1個用於特徵,1個用於小時。這裏是我的代碼:爲什麼我的MySQL結果意外重複?

$query = mysqli_query($connection, " 
SELECT `modelHomes`.`id`, 'testname' as `name`, `modelHomes`.`sqft`, 
    '123 Fake St' as `address`, '555-5555' as `phone`, `modelHomes`.`galleryid`, 
    `modelHomes`.`virtualtourid`, `modelHomeFeatures`.`text` as `feature`, 
    `modelHomeHours`.`text` as `hour` 
FROM `modelHomes` 
INNER JOIN `modelHomeFeatures` ON `modelHomeFeatures`.`modelhomeid` = `modelHomes`.`id` 
INNER JOIN `modelHomeHours` ON `modelHomeHours`.`modelhomeid` = `modelHomes`.`id` 
"); 

       $results = array(); 
       while($row = mysqli_fetch_assoc($query)){ 

         $results[] = $row; 

       } 
       return $results; 

它返回:

Array ([0] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => 4 Bedrooms [hour] => Mon-Thurs 1pm-6pm) [1] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => 4 Bedrooms [hour] => Friday 12pm-5pm) [2] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => 4 Bedrooms [hour] => Sat-Sun 12pm-5pm) [3] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => 2 Sided Gas Fireplace [hour] => Mon-Thurs 1pm-6pm) [4] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => 2 Sided Gas Fireplace [hour] => Friday 12pm-5pm) [5] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => 2 Sided Gas Fireplace [hour] => Sat-Sun 12pm-5pm) [6] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => All Oak Staircase with Open Risers [hour] => Mon-Thurs 1pm-6pm) [7] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => All Oak Staircase with Open Risers [hour] => Friday 12pm-5pm) [8] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => All Oak Staircase with Open Risers [hour] => Sat-Sun 12pm-5pm) [9] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Upgraded Hardwood Flooring & Berber Carpet [hour] => Mon-Thurs 1pm-6pm) [10] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Upgraded Hardwood Flooring & Berber Carpet [hour] => Friday 12pm-5pm) [11] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Upgraded Hardwood Flooring & Berber Carpet [hour] => Sat-Sun 12pm-5pm) [12] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Upgraded Ceramic Backsplash [hour] => Mon-Thurs 1pm-6pm) [13] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Upgraded Ceramic Backsplash [hour] => Friday 12pm-5pm) [14] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Upgraded Ceramic Backsplash [hour] => Sat-Sun 12pm-5pm) [15] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => 9' Main Floor Ceilings [hour] => Mon-Thurs 1pm-6pm) [16] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => 9' Main Floor Ceilings [hour] => Friday 12pm-5pm) [17] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => 9' Main Floor Ceilings [hour] => Sat-Sun 12pm-5pm) [18] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Fruit Cellar Under Porch [hour] => Mon-Thurs 1pm-6pm) [19] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Fruit Cellar Under Porch [hour] => Friday 12pm-5pm) [20] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Fruit Cellar Under Porch [hour] => Sat-Sun 12pm-5pm) [21] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Upgraded Wrap Around Porch [hour] => Mon-Thurs 1pm-6pm) [22] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Upgraded Wrap Around Porch [hour] => Friday 12pm-5pm) [23] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Upgraded Wrap Around Porch [hour] => Sat-Sun 12pm-5pm))

我不知道爲什麼結果不斷重複。我試圖調整我的代碼像這樣:

$results['hours'][] = $row['hour']; 
$results['features'][] = $row['feature']; 
$results['details'][] = $row; 

但我的結果不斷重複。這是我得到的輸出:

Array ([hours] => Array ([0] => Mon-Thurs 1pm-6pm [1] => Friday 12pm-5pm [2] => Sat-Sun 12pm-5pm [3] => Mon-Thurs 1pm-6pm [4] => Friday 12pm-5pm [5] => Sat-Sun 12pm-5pm [6] => Mon-Thurs 1pm-6pm [7] => Friday 12pm-5pm [8] => Sat-Sun 12pm-5pm [9] => Mon-Thurs 1pm-6pm [10] => Friday 12pm-5pm [11] => Sat-Sun 12pm-5pm [12] => Mon-Thurs 1pm-6pm [13] => Friday 12pm-5pm [14] => Sat-Sun 12pm-5pm [15] => Mon-Thurs 1pm-6pm [16] => Friday 12pm-5pm [17] => Sat-Sun 12pm-5pm [18] => Mon-Thurs 1pm-6pm [19] => Friday 12pm-5pm [20] => Sat-Sun 12pm-5pm [21] => Mon-Thurs 1pm-6pm [22] => Friday 12pm-5pm [23] => Sat-Sun 12pm-5pm) [features] => Array ([0] => 4 Bedrooms [1] => 4 Bedrooms [2] => 4 Bedrooms [3] => 2 Sided Gas Fireplace [4] => 2 Sided Gas Fireplace [5] => 2 Sided Gas Fireplace [6] => All Oak Staircase with Open Risers [7] => All Oak Staircase with Open Risers [8] => All Oak Staircase with Open Risers [9] => Upgraded Hardwood Flooring & Berber Carpet [10] => Upgraded Hardwood Flooring & Berber Carpet [11] => Upgraded Hardwood Flooring & Berber Carpet [12] => Upgraded Ceramic Backsplash [13] => Upgraded Ceramic Backsplash [14] => Upgraded Ceramic Backsplash [15] => 9' Main Floor Ceilings [16] => 9' Main Floor Ceilings [17] => 9' Main Floor Ceilings [18] => Fruit Cellar Under Porch [19] => Fruit Cellar Under Porch [20] => Fruit Cellar Under Porch [21] => Upgraded Wrap Around Porch [22] => Upgraded Wrap Around Porch [23] => Upgraded Wrap Around Porch) [details] => Array ([0] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => 4 Bedrooms [hour] => Mon-Thurs 1pm-6pm) [1] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => 4 Bedrooms [hour] => Friday 12pm-5pm) [2] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => 4 Bedrooms [hour] => Sat-Sun 12pm-5pm) [3] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => 2 Sided Gas Fireplace [hour] => Mon-Thurs 1pm-6pm) [4] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => 2 Sided Gas Fireplace [hour] => Friday 12pm-5pm) [5] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => 2 Sided Gas Fireplace [hour] => Sat-Sun 12pm-5pm) [6] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => All Oak Staircase with Open Risers [hour] => Mon-Thurs 1pm-6pm) [7] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => All Oak Staircase with Open Risers [hour] => Friday 12pm-5pm) [8] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => All Oak Staircase with Open Risers [hour] => Sat-Sun 12pm-5pm) [9] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Upgraded Hardwood Flooring & Berber Carpet [hour] => Mon-Thurs 1pm-6pm) [10] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Upgraded Hardwood Flooring & Berber Carpet [hour] => Friday 12pm-5pm) [11] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Upgraded Hardwood Flooring & Berber Carpet [hour] => Sat-Sun 12pm-5pm) [12] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Upgraded Ceramic Backsplash [hour] => Mon-Thurs 1pm-6pm) [13] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Upgraded Ceramic Backsplash [hour] => Friday 12pm-5pm) [14] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Upgraded Ceramic Backsplash [hour] => Sat-Sun 12pm-5pm) [15] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => 9' Main Floor Ceilings [hour] => Mon-Thurs 1pm-6pm) [16] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => 9' Main Floor Ceilings [hour] => Friday 12pm-5pm) [17] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => 9' Main Floor Ceilings [hour] => Sat-Sun 12pm-5pm) [18] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Fruit Cellar Under Porch [hour] => Mon-Thurs 1pm-6pm) [19] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Fruit Cellar Under Porch [hour] => Friday 12pm-5pm) [20] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Fruit Cellar Under Porch [hour] => Sat-Sun 12pm-5pm) [21] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Upgraded Wrap Around Porch [hour] => Mon-Thurs 1pm-6pm) [22] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Upgraded Wrap Around Porch [hour] => Friday 12pm-5pm) [23] => Array ([id] => 1 [name] => testname [sqft] => 2501 [address] => 123 Fake St [phone] => 555-5555 [galleryid] => 9 [virtualtourid] => 1 [feature] => Upgraded Wrap Around Porch [hour] => Sat-Sun 12pm-5pm))) 

如何在1個數組內沒有數據重複的情況下將結果轉換爲3個數組?

這是當我進入查詢到phpMyAdmin的我得到的結果:

enter image description here

我所尋找的是1個陣列ID,姓名,平方英尺,地址,電話,galleryid和virtualtourid, 1個具有特徵(不重複)的陣列和1個具有小時(不重複)的陣列。

+0

結果重複,因爲你的查詢返回結果重複。這種代碼無法將數組推動到數組中,除非結果是從頭開始的。你是否手動運行該查詢並查看結果是什麼? –

+0

是的,我已經在phpMyAdmin運行此查詢,我的結果是我在圖片的形式問題。 – user3723240

+1

如果你正確格式化您的數組轉儲,你會看到他們是** **不重複。'小時'值正在改變,就像它們在你的screencap中一樣。 –

回答

1

我會用GROUP_CONCAT分組的時間:

選擇modelHomesid, '測試名' 爲namemodelHomessqft, '123假聖' 爲address, '555-5555' 爲phonemodelHomesgalleryidmodelHomesvirtualtouridmodelHomeFeaturestextfeature, GROUP_CONCAT(modelHomeHourstext)作爲hour FROM modelHomes INNER JOIN modelHomeFeatures ON modelHomeFeaturesmodelhomeid = modelHomesid INNER JOIN modelHomeHours ON modelHomeHoursmodelhomeid = modelHomesid GROUP BY modelHomes.id

試試這個

$query = mysqli_query($connection,"SELECT modelHomes.id, 'testname' as name, modelHomes.sqft,'123 Fake St' as address, '555-5555' as phone, modelHomes.galleryid,modelHomes.virtualtourid, 
             modelHomeFeatures.text as feature,GROUP_CONCAT(modelHomeHours.text) as hour 
            FROM modelHomes 
           INNER JOIN modelHomeFeatures ON modelHomeFeatures.modelhomeid = modelHomes.id 
           INNER JOIN modelHomeHours ON modelHomeHours.modelhomeid = modelHomes.id 
          GROUP BY modelHomes.id"); 
$results = array(); 
while($row = mysqli_fetch_assoc($query)){ 
    $ahours=explode(",",$row["hour"]); 
    array_push($results,array(id=>$row["id"], 
           name=>$row["name"], 
           sqft=>$row["sqft"], 
           address=>$row["address"], 
           phone=>$row["phone"], 
           galleryid=>$row["galleryid"], 
           virtualtourid=>$row["virtualtourid"], 
           feature=>$row["feature"], 
           hour=>$ahours)); 
} 
return $results; 

ANOTHER的形式給出了查詢

SELECT modelHomes.id, 'testname' as name, modelHomes.sqft, 
      '123 Fake St' as address, '555-5555' as phone, 
      modelHomes.galleryid,modelHomes.virtualtourid, 
      (SELECT GROUP_CONCAT(modelHomeFeatures.text) 
       FROM modelHomeFeatures 
      WHERE modelHomeFeatures.modelhomeid = modelHomes.id) as feature, 
      (SELECT GROUP_CONCAT(modelHomeHours.text) 
       FROM modelHomeHours 
      WHERE modelHomeHours.modelhomeid = modelHomes.id) as hour 
     FROM modelHomes 
    GROUP BY modelHomes.id 
+0

那不是我在找什麼...我不認爲我的問題是與我的查詢,我認爲它更多的在PHP方面,我怎麼用我的結果 – user3723240

+0

我唯一的問題是,當我GROUP_CONCAST(modelHomeFeatures.text )的功能重複 – user3723240

+0

嘗試其他方法查詢 – bitsm