2014-10-06 92 views
-1

這是我的代碼,結果是錯誤的。我想顯示第一列中的第一個查詢的結果和第二列中的第二個查詢的結果,但是兩個結果都放在一個列中(第一列)。像這樣而不改變查詢?如何在一個表中顯示mysql的兩個查詢的兩個結果?

/* 
    username  username 
    username_a_1 username_b_1 
    username_a_1 username_b_4 
    username_a_2 username_b_1 
    username_a_2 username_b_4 
    username_a_3 username_b_5 
    username_a_4 username_b_2 
    username_a_4 username_b_3 
    username_a_5 username_b_1 
    username_a_5 username_b_4 
*/ 
<html> 
<head></head> 
<table border="1" > 
    <tr> 
     <th>USERNAME</th> 
     <th>USERNAME</th> 
    </tr> 
<?php 
include'db_connect.php'; 

$query1='SELECT username FROM contacts_a '; 
$query_run1=mysql_query($query1); 

$query2="SELECT contacts_a.username,contacts_b.username FROM contacts_a LEFT JOIN contacts_b ON contacts_a.level=contacts_b.level"; 
$query_run2=mysql_query($query2); 



while($query_array1=mysql_fetch_assoc($query_run1)){ 
     foreach($query_array1 as $index => $names){ 

      echo '<tr> 
         <td>'.(($names == NULL)? 'NULL': $names).'</td> 
        </tr>'; 
     }//end of foreach 

    }//end of while 

    while($query_array2=mysql_fetch_assoc($query_run2)){ 

     foreach($query_array2 as $index => $names){ 

      echo '<tr> 
         <td>'.(($names == NULL)? 'NULL': $names).'</td> 
        </tr>'; 
     }//end of foreach 

    }//end of while 




?> 
</table> 

</html> 
+5

請不要使用'mysql_ *'函數](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。 *他們不再維護,並[已正式棄用](https://wiki.php.net/rfc/mysql_deprecation)*。學習[準備的語句](http://en.wikipedia.org/wiki/Prepared_statement),並使用[PDO](http://us1.php.net/pdo)或[MySQLi](http:// us1.php.net/mysqli)。 [本文](http://php.net/manual/en/mysqlinfo.api.choosing.php)將幫助你決定。 – 2014-10-06 18:12:15

+1

您已在第二個查詢中擁有所有需要的數據:contacts_a.username是第一個查詢的用戶名,contacts_b.username是第二個查詢的用戶名。只需循環第二組數據即可打印到您的表格。 – Mark 2014-10-06 18:14:09

+0

...並向該查詢中添加一個ORDER BY'子句,以獲取您指定的序列中返回的行,而不是允許數據庫以任意順序返回行。 – spencer7593 2014-10-06 19:01:29

回答

0

試試這個:

// Select the data, all together now. The difference is we'll give it a name 
$query2="SELECT contacts_a.username as firstTableName, 
       contacts_b.username as secondTableName 
      FROM contacts_a 
     LEFT JOIN contacts_b 
      ON contacts_a.level = contacts_b.level"; 

// Execute the Query 
$query_run2=mysql_query($query2); 

// Loop over our results... 
while($query_array1 = mysql_fetch_assoc($query_run1)) { 
    // We're going to use this again, give it a good name 
    $firstName = ($query_array1['firstTableName'] == NULL) ? 'NULL' : 
     $query_array1['firstTableName']; 
    $secondName = ($query_array1['secondTableName'] == NULL) ? 'NULL' : 
     $query_array1['secondTableName']; 

    // Put out a new table row 
    echo '<tr>'; 
     // And our first TD... 
     echo '<td>' . firstName .'</td>'; 
     echo '<td>' . secondName .'</td>'; 
    echo '</tr>'; 

// End our loop 
} // So Long and Thanks for All the Fish! 

一些其他的想法:Don't use MySQL!你也可以擺脫現在第一次查詢的,因爲它是多餘的。關於你的NULL檢查,如果數據以空字符串的形式出現,會發生什麼?通常我會檢查null和空引號:「'或」「,以確保我能夠找回我想要的內容。免責聲明:我沒有真正嘗試這個,我使用PDO,所以我的MySQL語法用於取回和讀取MySQL數組可能是不正確的!

+0

感謝您的回答,但我的大問題是如何填寫兩列表的同時,當我有2個查詢,而你寫的while循環沒有做到這一點 – 2014-10-06 19:31:44

-2
$qa1 = mysql_fetch_assoc($query_run1); 
$qa2 = mysql_fetch_assoc($query_run2); 

do { 
    echo '<tr> 
    <td>'.(current($qa1) == NULL ? 'NULL': current($qa1)).'</td> 
    <td>'.(current($qa2) == NULL ? 'NULL': current($qa2)).'</td> 
    </tr>'; 
} while(next($qa1) || next($qa2)); 
相關問題