gpt4 book ai didi

准备好的语句中的 PHP 准备好的语句

转载 作者:可可西里 更新时间:2023-11-01 07:46:46 24 4
gpt4 key购买 nike

我正在观看有关使用数据库制作菜单的视频教程。我没有像视频中那样使用程序化 PHP,而是尝试使用准备好的语句 OOP 样式来完成。它不起作用,我不明白为什么。

它运行良好,直到第 17 行,在那里它死于这个错误:

fatal error :在第 17 行对 C:\wamp\www\widget_corp\content.php 中的非对象调用成员函数 bind_param()

代码如下:

    <?php
$query = $connection->prepare('SELECT menu_name, id FROM subjects ORDER BY position ASC;');
$query->execute();
$query->bind_result($menu_name, $sid);

while ($query->fetch()){
echo "<li>{$menu_name} {$sid}</li>";

$query2 = $connection->prepare('SELECT menu_name FROM pages WHERE subject_id = ? ORDER BY position ASC;');
$query2->bind_param("i", $sid); //This is line 17
$query2->execute();
$query2->bind_result($menu_name);
echo "<ul class='pages'>";
while ($query2->fetch()){
echo "<li>{$menu_name}</li>";
}
echo "</ul>";

}
$query->close();
?>

是否不可能在 stmt->fetch(); 中做准备语句?

最佳答案

想通了:

执行并绑定(bind)结果后,必须将其存储(如果要将另一个准备好的语句放入提取中)。因此,在这种情况下,必须从缓冲结果中读取数据。

换句话说,在对同一连接进行提取之前,不能执行另一个查询。

工作代码:

$query = $connection->prepare("SELECT menu_name, id FROM subjects ORDER BY position ASC;");
$query->execute();
$query->bind_result($menu_name, $sid);
$query->store_result();

关于准备好的语句中的 PHP 准备好的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5579557/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com