【问题标题】:How to show two results of two queries of mysql in one table?如何在一张表中显示两个mysql查询的两个结果?
【发布时间】:2018-05-30 23:37:38
【问题描述】:

这是我的代码,结果是错误的。我想在第一列中显示我的第一个查询的结果,在第二列中显示第二个查询的结果,但两个结果都在一个(第一)列中。我怎么能在不改变查询的情况下拥有这样的结果?

/*
    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>

【问题讨论】:

  • 拜托,don't use mysql_* functions它们不再被维护并且是officially deprecated。改为了解prepared statements,并使用PDOMySQLiThis article 将帮助您做出决定。
  • 您已经拥有第二个查询所需的所有数据:contacts_a.username 是第一个查询的用户名,contacts_b.username 是第二个查询的用户名。只需循环第二组数据即可打印到您的表格中。
  • ... 并在该查询中添加一个ORDER BY 子句,以获取按您指定的顺序返回的行,而不是允许数据库以它想要的任何顺序返回行。跨度>

标签: php mysql html-table


【解决方案1】:

试试这个:

// 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 语法可能不正确!

【讨论】:

  • 感谢您的回答,但我最大的问题是当我有 2 个查询并且您编​​写的 while 循环不这样做时,如何同时填充两列表格
【解决方案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) );

【讨论】:

    猜你喜欢
    • 2014-08-17
    • 2017-08-11
    • 2012-12-26
    • 2019-03-27
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多