Mysqli 多查询错误


Mysqli multi query error

我需要一次执行两个或多个查询,一个接一个地获取它们。

然后我需要绑定参数并绑定每个参数的结果。

我正在尝试这种方式,但出现错误。

$query="
SELECT 
`date`, `ip`, AVG(`value`)
FROM `stats`.`stats`    
WHERE `uid`=?
GROUP BY `date`, `ip`;
";
$query.="
SELECT 
`category`, `ip`, AVG(`value`)
FROM `stats`.`stats`    
WHERE `uid`=?
GROUP BY `category`, `ip`;
";
$prepare = mysqli_multi_query($connection, $query);
mysqli_stmt_bind_param($prepare, 'ss', $uid,$uid);
mysqli_stmt_execute($prepare) or trigger_error(mysqli_error($connection), E_USER_ERROR);

然后在 html 部分中,我需要一个接一个地获取这些结果

//fetch first query result
while (mysqli_stmt_fetch($prepare)):
mysqli_stmt_store_result($prepare);
mysqli_stmt_bind_result($prepare, $date,$ip,$value);
mysqli_free_result($result);
endwhile;
//fetch second query result
while (mysqli_next_result($prepare)):
while (mysqli_stmt_fetch($prepare));
mysqli_stmt_store_result($prepare);
mysqli_stmt_bind_result($prepare, $category,$ip,$value);
mysqli_free_result($result);
endwhile;

但它无法显示多个错误。

 Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in
 Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean given in
 Fatal error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? GROUP BY `date`,`ip` ) ..

请查看并建议任何可能的方法。

谢谢

我需要一次执行两个或多个查询

你没有。

因此,您应该只使用常规的准备/执行查询,逐个运行它们。可以添加事务以提高速度和一致性

$data = [
    ['col1' => 'foo1', 'col2' => 'bar1'],
    ['col1' => 'foo2', 'col2' => 'bar2'],
];
// prepare SQL query once
$stmt = $mysqli->prepare("INSERT INTO table SET col1 = ?, col2 = ?");
$mysqli->begin_transaction();
// loop over the data array
foreach ($data as $row) {
    $stmt->bind_param("ss", $row['col1'], $row['col2']);
    $stmt->execute();
}
$mysqli->commit();