【问题标题】:Mysql - How to query a mysqli_multi_query() to return the entire rowMysql - 如何查询 mysqli_multi_query() 以返回整行
【发布时间】:2014-12-08 08:19:47
【问题描述】:

我的选择:

$query  = "SELECT * FROM user where userID='1';";

在我的 mysqli_multi_query() 中没有返回整行,它只返回一列。

这是我的结果:

1-----------------book1 book3 book4

但这就是我想要的:

1 John Jan-26 4-----------------book1 book3 book4

这是我的两张桌子:

Table1: user

|    userID   |     name        |   date       |   booksRead
---------------------------------------------------------------------
|    1        |     John        |   Jan-26     |   4
|    2        |     Andy        |   Jan-27     |   7
|    3        |     Mark        |   Jan-28     |   8

Table 2: booksCheckedOut

row    |   userID   |      book    |  date
-------------------------------------------------
1      |    1       |      book1   |  Jan-26
2      |    2       |      book2   |  Jan-27
3      |    1       |      book3   |  Jan-28
4      |    1       |      book4   |  Jan-29

这是我的 php 代码:

<?php
$mysqli = new mysqli("localhost","root","password","database_name");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query  = "SELECT * FROM user where userID='1';";
$query .= "SELECT book FROM booksCheckedOut where userID='1';";


if ($mysqli->multi_query($query)) {    // execute multi query
    do {
        if ($result = $mysqli->store_result()) {     // store first result
            while ($row = $result->fetch_row()) {
                printf("%s\n", $row[0]);
            }
            //$result->close();
        }

        if ($mysqli->more_results()) {    /* print divider */
            printf("-----------------\n");
        }
    } while ($mysqli->next_result());
}

$mysqli->close();
?>

有谁知道我做错了什么?

提前致谢!

【问题讨论】:

  • 你的userID在数据库中可能是整数,在你的查询中不需要用引号封装。所以在查询中将'1' 更改为1 =)
  • @RichardBernards 知道了,谢谢!

标签: php mysql mysqli-multi-query


【解决方案1】:

根据您指定的输出,连接是继续的方式。

看看这是否有帮助
$query = select u.*, b.book from users u LEFT JOIN booksCheckedOut b ON u.userID = b.userID;

【讨论】:

  • 感谢您的回复。我宁愿将它们保留在两个单独的查询中,而不是加入它们。
【解决方案2】:

我希望这会奏效

select U.userID,U.name,U.date,U.booksRead,BCO.book from user U,booksCheckedOut BCO where U.userID=BCO.userID  

【讨论】:

    【解决方案3】:

    试试这个简单的查询:

    SELECT u.*, b.book FROM users AS u INNER JOIN booksCheckedOut  AS b ON u.userID = b.userID;
    

    【讨论】:

      【解决方案4】:

      Query1:“SELECT * FROM user where userID='1';”返回 1 行 4 列。

      Query2: "SELECT book FROM booksCheckedOut where userID='1';"返回 3 行 1 列

      如果您想输出 Query1 中的所有列,那么您应该使用:

      printf("%s %s %s %s", $row[0]);
      

      来自 Query2 的所有三行(仅包含一列)都通过以下方式迭代和输出:

      printf("%s", $row[0]);
      

      所以 printf() 中断了您的输出,但老实说,您应该使用 LEFT JOIN,因为您使用相同的 id 引用两个表。如果您不熟悉 JOIN,请熟悉一下,因为您的情况需要它。

      【讨论】:

        猜你喜欢
        • 2011-09-02
        • 1970-01-01
        • 1970-01-01
        • 2010-10-11
        • 1970-01-01
        • 1970-01-01
        • 2014-01-18
        • 1970-01-01
        相关资源
        最近更新 更多