mysqli查询只返回每个表中的一行

时间:2013-10-27 06:59:46

标签: mysqli multi-query

我终于设法让我的代码从两个表中的每一个返回第一列,但是如何更改代码以从两个表返回所有各种列数据,并按ID号顺序返回。这是我的代码

<?php
$mysqli = mysqli_connect("localhost", "name", "pass", "db");
// check connection
    if (mysqli_connect_errno()) {
      echo "Connect failed: " . mysqli_connect_errno(); exit();
    }

$query = "SELECT * FROM custrec;"; "SELECT * FROM contidr;";

$result = array();


/* execute multi query */
if ($mysqli->multi_query($query))
{
    do 
  {
      //store first result set
      if($result = $mysqli->store_result()) 
     {
         while( $rows = $result->fetch_row())
         {
               printf("<br/>%s<br/>", $rows[0]);
         }
             $result->free();
     }
        /* print divider */

        if($mysqli->more_results())
        {
            printf("-----------------<br/>");
        }
        else
        {
            echo '<br/>';
        }
} while($mysqli->more_results() && $mysqli->next_result());
}

/* close connection */
$mysqli->close();
?>

1 个答案:

答案 0 :(得分:0)

multi_query部分没有任何问题。

您将两个查询分配给$ query时出错。

您:

$query = "SELECT * FROM custrec;"; "SELECT * FROM contidr;";

就像是说:

$query = "SELECT * FROM custrec;";
"SELECT * FROM contidr;";

第二个预期查询未附加到$ query。

解决方案是: 一个字符串(首选):

$query = "SELECT * FROM custrec;SELECT * FROM contidr";

或两个串联的字符串:

$query = "SELECT * FROM custrec;"."SELECT * FROM contidr";