1

我执行以下操作:

$query .=" SELECT * , COUNT(PRESENT) FROM seventh_a";
$query .=" WHERE class ='0' AND NAME ='Alexander Kirkby Scherer'";
$query .=" AND PRESENT = 'TS' GROUP BY lesson";

$query = " SELECT * , COUNT(lesson) FROM seventh_a";
$query .=" WHERE class ='0' AND NAME ='Alexander Kirkby Scherer'";
$query .=" GROUP BY lesson";

之后我可以回显$row['COUNT(PRESENT)']但不能$row['COUNT(lesson)']

谁能告诉我如何获得这两个值,以便我能够使用它们?

4

2 回答 2

2

根据手册页,如果您使用 mysqli_multi_query,您可以访问不同查询的结果,如下所示:

要从第一个查询中检索结果集,您可以使用mysqli_use_result()mysqli_store_result()。所有后续查询结果都可以使用 mysqli_more_results()和进行处理mysqli_next_result()

他们提供了一些示例代码:

$query  = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";

/* execute multi query */
if ($mysqli->multi_query($query)) {
    do {
        /* store first result set */
        if ($result = $mysqli->store_result()) {
            while ($row = $result->fetch_row()) {
                printf("%s\n", $row[0]);
            }
            $result->free();
        }
        /* print divider */
        if ($mysqli->more_results()) {
            printf("-----------------\n");
        }
    } while ($mysqli->next_result());
}
于 2012-10-05T15:02:50.863 回答
1

您不能使用 2 query 。像这样使用组合查询

$query .=" SELECT * , COUNT(PRESENT),COUNT(lesson) FROM seventh_a";
$query .=" WHERE class ='0' AND NAME ='Alexander Kirkby Scherer'";
$query .=" AND PRESENT = 'TS' GROUP BY lesson";

你会得到你的结果$row['COUNT(PRESENT)']$row['COUNT(lesson)']

或者使用mysqli的multi_query函数在这样的;查询之间添加

$query .=" SELECT * , COUNT(PRESENT) FROM seventh_a";
$query .=" WHERE class ='0' AND NAME ='Alexander Kirkby Scherer'";
$query .=" AND PRESENT = 'TS' GROUP BY lesson;";   // semicolon here

$query .= " SELECT * , COUNT(lesson) FROM seventh_a";
$query .=" WHERE class ='0' AND NAME ='Alexander Kirkby Scherer'";
$query .=" GROUP BY lesson";
于 2012-10-05T14:54:43.473 回答