2016-01-22 40 views
-1

如果我在PHP中使用下面的行的MySQL查詢工作完全正常:INNER JOIN與*,但不是一個單一的野外工作

$query = " 
SELECT * 
    FROM customers_1 
    JOIN customers_2 
    ON customers_1.id = customers_2.cus_id; 
"; 

但是當我嘗試只得到兩個單場這樣的:

$query = " 
SELECT customers_1.product,customers_1.id 
    FROM customers_1 
    JOIN customers_2 
    ON customers_1.id = customers_2.cus_id; 
"; 

它根本不工作,意味着我沒有得到任何輸出。當我複製並粘貼第二個查詢並將其放入PHPMyAdmin時,它工作得很好。因爲你只是fetch one column從查詢SELECT customers_1.product..

$result = mysql_query($query); 

while($row = mysql_fetch_array($result)) 
{ 
    if($row['id']=="3") 
    { 
     foreach($row as $field) 
     { 
      echo $field; 
     } 
    } 
} 

mysql_close($dbhandle); 
+0

檢查兩個表中是否存在「產品」列。 – Pupil

+2

查詢中的id字段($ row ['id'])在哪裏? –

+2

mysql_ *擴展在PHP 5.5.0中被棄用。 [MySQLi](http://php.net/manual/en/book.mysqli.php)或[PDO_MySQL](http://php.net/manual/en/ref.pdo-mysql.php)擴展應該使用 –

回答

0

您比較id在你的查詢檢索customers_1.product時,在你的代碼中。

+0

即時對不起,我編輯原來的這篇文章,並在改變了一個錯誤,我得到當然ID的課程 – user3877230

1

因爲$row['id']是沒有在你的代碼,並且列product

而且無需foreach循環:

我做什麼之後,這是。只要讓你的價值在一個while循環

更新答題更新後

while ($row = mysql_fetch_array($result)) { 
      if (!empty($row['id'])) { 
       echo $row['id']; 
      } 
     } 

注: - MySQL是不推薦使用,而不是使用庫MySQLi或PDO

+0

即時對不起,我編輯原來的這篇文章,並在改變了一個錯誤,我得到的字段編號當然 – user3877230