关于mysqli:PHP注意:试图在第8行的post.php中获取非对象的属性

PHP Notice: Trying to get property of non-object in post.php on line 8

本问题已经有最佳答案,请猛点这里访问。

我收到此错误

PHP Notice: Trying to get property of non-object in post.php on line
8

在下面的代码上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
if (is_numeric($_GET["id"])) {
    $mysqli=mysqli_connect("localhost","xxx","xxx","xxx");
    $query = $mysqli->query("SELECT n FROM table WHERE o=".$_GET["id"]);
    if ($query) {
        if ($query->num_rows === 0) {
                header("HTTP/1.1 301 Moved Permanently");
                header("Location: http://www.example.com/?p=".$query->fetch_object()->n);
        }
    }
    mysqli_close($mysqli);
}
else {
    echo $_GET["id"];

}
?>

知道发生了什么吗?我假设我进行的检查将确保仅通过有效数据

谢谢

相关讨论

  • 这里 fetch_object() 不是这里的对象。像 while ($obj = $result->fetch_object()) { 一样使用 fetch_object() 然后使用 $obj->n
  • 你有 num_rows === 0 然后尝试从 0 行中获取一个对象
  • @BasheerAhmed 他的表有一个不同的名称,这只是简化了,如下面的问题评论中所述。此外,900 != 9k 😉
  • @TanuelMategi如果不是这样,那么他为什么接受它作为答案..
  • @BasheerAhmed 主要答案是 while loop 检查 updated 部分答案
  • @BasheerAhmed 然后看看评论:It's not actually table in my proper code - didn't want to give out my real table name – pee2pee 更新被接受了

table 是 mysql 中的保留关键字,它必须在 backtict 中。您的查询未能给出结果,这就是您收到此错误的原因

1
SELECT n FROM `table` WHERE o=".$_GET["id"]

更新

为了获取数据,你必须使用 while 循环

1
2
3
while ($obj = $query->fetch_object()) {
            header("Location: http://www.example.com/?p=".$obj->n);
    }

检查 http://php.net/manual/en/mysqli-result.fetch-object.php

相关讨论

  • 它实际上不是我正确代码中的表格 - 不想给出我的真实表格名称
  • PHP 解析错误:语法错误,意外 \'->\'
  • 啊,我的错误忘记了 obj 检查中的 $ 已更新
  • D\'oh - 我也应该看到它

您正在尝试处理空结果。你问结果是否有 0 行,然后尝试获取一行结果必须是 0 行:

1
2
3
4
if ($query->num_rows === 0) { // num_rows === 0
    header("HTTP/1.1 301 Moved Permanently");         //fetch with 0 rows?
    header("Location: http://www.example.com/?p=".$query->fetch_object()->n);
}

相关讨论

  • 试过了,但不管什么原因都没有

以上是关于mysqli:PHP注意:试图在第8行的post.php中获取非对象的属性的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>