0

我正在尝试了解多查询如何在 mysqli 中工作。但我承认这并不容易理解。

基本上我如何在多查询中进行这些查询?该页面没有讨论多查询中的准备好的语句。

($sql = $db -> prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)"));
$sql -> bind_param('sss', $name, $email, $hash);
$sql -> execute();

($sq = $db -> prepare("INSERT INTO activation_link_password_reset  (activation_link) VALUES (?)"));
$sq -> bind_param('s', $linkHash);
$sq -> execute();
4

2 回答 2

2

你不能在那里使用准备好的语句,而且加速也可以忽略不计,所以去更容易调试单独的查询。

如果您真的想在 1 次调用中使用准备好的语句来执行此操作,请创建一个PROCEDURE(更难调试...),然后准备CALL(:param1,:param2);.

于 2011-06-08T17:33:02.077 回答
-1

实际上,我几天前才弄清楚这一点。迁移到 MySQLi 并非易事,但我会继续向您展示准备好的语句的查询循环。

$db = new mysqli(...)

$statement = $db->prepare('SELECT count(*) as [hitchhikers] FROM `T_Users` WHERE `id` > ? AND `signin` like ?');
$statement->bind_param("ss", 42, '%adams');

if ($statement->execute())
{
    $statement->store_result();
    $statement->bind_result($hitchhikers);
    $statement->fetch();

    // do something with hitchhikers here
}
else
{
    // there was a problem with the execution, check $db->error
}

$statement->close();
$db->next_result();

Everything here works similar to the non oo format by replacing -> with _ and passing the returned value into the new function, but this process here is pretty much what you should look to be doing with each of those statements. You can abstract it away with a mysqli wrapper class as well.

After the $db->next_result() call you are able to repeat the process again. This change has to do with the way the MySQL connection and results are handled with MySQLi. You can, of course, avoid this issue entirely by switching back to mysql_ queries and resources.

Specifically for multi_query, you separate your queries with a semicolon, in a single string. The example you linked to shows how to process the results.

MySQLi Book @PHP.net

于 2011-06-08T17:35:57.950 回答