繁体   English   中英

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM