gpt4 book ai didi

php - 在非对象上调用成员函数 fetchObject()

转载 作者:行者123 更新时间:2023-12-04 05:35:42 24 4
gpt4 key购买 nike

在我的 while 循环中中断。一直在阅读 php 手册,但无法弄清楚我在这里做错了什么......

$pdo 调用我的数据库连接

function getSelectedPhoto($the_selected_id) {
global $pdo;

$id = $the_selected_id;

$stmt = $pdo->prepare("SELECT handle, src FROM images WHERE id = :id LIMIT 1");
$vars = array(':id' => $id);
$result = $stmt->execute($vars);

if($result) {
while($row = $result->fetchObject()) {
echo '<img src="../' . $row->src . '" alt="image" /><br />';
echo '<p id="handle">' . $row->handle . '</p>';
}
} else die("There was some problem");

}

最佳答案

根据手册“PDOStatement::executeexecute() -方法返回truefalse而不是结果:

Returns TRUE on success or FALSE on failure.



当然,这会在调用 $result->fetchObject() 时导致“在非对象上调用成员函数 fetchObject()”。 .

你的代码应该是这样的:
$result = $stmt->execute($vars);

if ($result) {
while( $row = $stmt->fetchObject() ) {}
}

关于php - 在非对象上调用成员函数 fetchObject(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11977774/

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