gpt4 book ai didi

php - 尝试获取非对象的属性 'num_rows'

转载 作者:行者123 更新时间:2023-11-29 10:16:55 28 4
gpt4 key购买 nike

有人可以帮我纠正这个代码吗?

我不断收到此错误:-

Notice: Trying to get property 'num_rows' of non-object

<?php

$test = $_GET['param'];
$sql =" SELECT * FROM img WHERE id = $test ";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo " <div class='col-lg-9'> ";

echo " <div class=card mt-4> ";
echo " <img src=http://placehold.it/900x400 class=img-responsive alt=Responsive image> ";
echo " <div class='card-body'> ";
echo " <a class=pull-right> <button type=button class='btn btn-primary'>Prezzo " .$row["prz"]. " €</button> </a> ";

echo " <h3 class=card-title>" .$row["nome"]. "</h3> " ;
echo "<br>";
echo "<br>";
echo " <p class=card-text>" .$row["ldscr"]. "</p> ";
echo "</div>";
echo "</div>";
echo "<br>";
}
}

?>

最佳答案

改变

if ($result->num_rows > 0) {

if (!empty($result) && $result->num_rows > 0) {

然后它就会起作用。

编辑:它不会爆炸的原因是,首先您要查看是否有任何结果。如果没有,那么它不会尝试显示它们(这是发生错误的地方)。

你真的应该更换

$sql =" SELECT * FROM img WHERE id = $test ";

$sql =" SELECT * FROM img WHERE id = ?";

然后使用 prepared statements 。但这是另一个问题。

关于php - 尝试获取非对象的属性 'num_rows',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50002520/

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