我正在尝试从数据库中删除一行。当我执行PHP脚本时,出现错误消息:
Notice: Trying to get property 'num_rows' of non-object in /home/send.php on line 46
。
有人知道我的脚本出了什么问题吗?
第46行= if ($result10->num_rows > 0) {
这是我用来删除行的PHP脚本:
$link10 = mysqli_connect("localhost", "root", "password", "database");
if($link10 === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql10 = "DELETE FROM statement WHERE id = '". $_SESSION['id'] ."' AND number = '". $_GET['number'] ."'";
$res10 = mysqli_query($link10, $sql10) ;
$result10 = $link10->query($sql10);
if ($result10->num_rows > 0) {
//script
}
答案 0 :(得分:1)
您已经在这里执行查询:
$res10 = mysqli_query($link10, $sql10);
因此您可以使用以下命令访问num_rows
:
if(mysqli_num_rows($res10) > 0)
并删除此行:
$result10 = $link10->query($sql10);
完整代码:
$link10 = mysqli_connect("localhost", "root", "password", "database");
if($link10 === false) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql10 = "DELETE FROM statement WHERE id = '". $_SESSION['id'] ."' AND number = '". $_GET['number'] ."'";
$res10 = mysqli_query($link10, $sql10);
if(mysqli_num_rows($res10) > 0) {
//script
}
答案 1 :(得分:1)
这就是你要做的
if ($res10->num_rows > 0) {
//script
}
请在下面尝试。 我已经注释了不需要的代码
$link10 = mysqli_connect("localhost", "root", "password", "database");
if($link10 === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql10 = "DELETE FROM statement WHERE id = '". $_SESSION['id'] ."' AND number = '". $_GET['number'] ."'";
$res10 = mysqli_query($link10, $sql10) ;
//$result10 = $link10->query($sql10);
if ($res10->num_rows > 0) {
//script
}
更新部分
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'database';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
if(! $conn ) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully<br>';
$sql = "DELETE FROM statement WHERE id = '". $_SESSION['id'] ."' AND number = '". $_GET['number'] ."'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
echo "deleted successfully";
} else {
echo " results cannot be deleted";
}
mysqli_close($conn);
?>
答案 2 :(得分:1)
错误消息表示 $ result10 不是对象,因此您不能将其用作对象
尝试一下:
$link10 = mysqli_connect("localhost", "root", "password", "database");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql10 = "DELETE FROM statement WHERE id = '". $_SESSION['id'] ."' AND number = '". $_GET['number'] ."'";
mysqli_query($link10, $sql10) ;
if (mysqli_affected_rows($link10) > 0) {
//script
}
以下是W3C
上的类似示例