php 尝试获取非对象 MySQLi 结果的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7332842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to get property of non-object MySQLi result
提问by Rick Hanshaw
Got a bit of PHP code I'm struggling with - had a search around Google etc. and tried everything mentioned, but for some reason I'm having trouble solving it.
得到了一些我正在努力解决的 PHP 代码 - 在谷歌等周围进行了搜索并尝试了提到的所有内容,但由于某种原因我无法解决它。
The problem is:
问题是:
I have some code that is querying a database for the presence of a particular user.
我有一些代码用于查询数据库是否存在特定用户。
The code (it's a method inside a class)
代码(它是类中的一个方法)
<?php
global $mysqli;
// Query specified database for value
$q = 'SELECT id FROM ' . $database . ' WHERE username = \'' . $username . '\'';
$r = $mysqli->query($q);
var_dump($r);
if ($r->num_rows) {
// If row found username exists so return false
return false;
}
...
?>
I've var dumped the result of the query ($r) and got this:
我已经 var 转储了查询的结果 ($r) 并得到了这个:
object(mysqli_result)#4 (5) { ["current_field"]=> int(0) ["field_count"]=> int(1) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) }
This is correct, there should only be 1 row as above.
这是正确的,上面应该只有 1 行。
I do get this error linking to the line saying if ($r->num_rows) {
我确实收到此错误链接到该行说 if ($r->num_rows) {
Notice: Trying to get property of non-object in FILE on line LINE
注意:尝试在 LINE 行上的 FILE 中获取非对象的属性
but I don't know why since the object is valid (as above) and it should be working fine. From what I can tell it seems to be going through alright, I'm just wondering why there's an error. I'm sure it's something simple but I'd appreciate any help.
但我不知道为什么,因为该对象是有效的(如上所述)并且它应该可以正常工作。据我所知,似乎一切正常,我只是想知道为什么会出现错误。我相信这很简单,但我很感激任何帮助。
回答by kemalatila
$sql = "SELECT * FROM table";
$result = $conn->query($sql);
if (!$result) {
trigger_error('Invalid query: ' . $conn->error);
}
check the error with mysqli_error() function
使用 mysqli_error() 函数检查错误
probably your query has some faults.
可能您的查询有一些错误。
回答by Oxygen Airy
The cause of your problem is simple. So many people will run into the same problem, Because I did too and it took me hour to figure out. Just in case, someone else stumbles, The problem is in your query, your select statement is calling $dbname
instead of table name. So its not found whereby returning false
which is boolean
. Good luck.
你的问题的原因很简单。这么多人会遇到同样的问题,因为我也遇到过,我花了一个小时才弄明白。以防万一,其他人绊倒了,问题出在您的查询中,您的 select 语句正在调用$dbname
而不是表名。所以它没有找到由此返回的false
是boolean
. 祝你好运。
回答by Sashi Kiran
I have been working on to write a custom module in Drupal 7 and got the same error:
我一直致力于在 Drupal 7 中编写自定义模块,但遇到了同样的错误:
Notice: Trying to get property of non-object
注意:尝试获取非对象的属性
My code is something like this:
我的代码是这样的:
function example_node_access($node, $op, $account) {
if ($node->type == 'page' && $op == 'update') {
drupal_set_message('This poll has been published, you may not make changes to it.','error');
return NODE_ACCESS_DENY;
}
}
Solution:
I just added a condition if (is_object($sqlResult))
, and everything went fine.
解决方案:我刚刚添加了一个条件if (is_object($sqlResult))
,一切顺利。
Here is my final code:
这是我的最终代码:
function mediaten_node_access($node, $op, $account) {
if (is_object($node)){
if ($node->type == 'page' && $op == 'update') {
drupal_set_message('This poll has been published, you may not make changes.','error');
return NODE_ACCESS_DENY;
}
}
}
回答by Xuwel Khan
I think thats not the reason everybody told above. There is something wrong in your code, maybe miss spelling or mismatching with the database column names. If mysqli query gets no result then it will return false, so that it is not a object - is a wrong idea. Everything works fine. it returns 1 or 0 if query have result or not.
我认为这不是上面每个人所说的原因。您的代码有问题,可能拼写错误或与数据库列名不匹配。如果 mysqli 查询没有结果,那么它将返回 false,因此它不是一个对象 - 是一个错误的想法。一切正常。如果查询有结果,则返回 1 或 0。
So, my suggestion is check your variable names and table column names or any other misspelling.
因此,我的建议是检查您的变量名和表列名或任何其他拼写错误。
回答by Relaxing In Cyprus
Just thought I would expand on this a bit.
只是想我会对此进行一些扩展。
If you perform a MYSQLI SELECT query that returns 0 results, it returns FALSE.
如果执行返回 0 结果的 MYSQLI SELECT 查询,则返回 FALSE。
However, if you get this error and you have written your own MYSQLI Query function, then you can also get this error if the query you are running is not a select but an update. An update query will return either TRUE or FALSE. So if you just assume that any non false result will have records returned, then you will trip up when you run an update or anything other than select.
但是,如果您收到此错误并且您编写了自己的 MYSQLI 查询函数,那么如果您正在运行的查询不是选择而是更新,您也会收到此错误。更新查询将返回 TRUE 或 FALSE。因此,如果您只是假设任何非错误结果都会返回记录,那么您将在运行更新或选择以外的任何操作时出错。
The easiest solution, once you have checked that its not false, is to first check that the result of the query is an object.
最简单的解决方案,一旦你检查它不是假的,就是首先检查查询的结果是一个对象。
$sqlResult = $connection->query($sql);
if (!$sqlResult)
{
...
}
else if (is_object($sqlResult))
{
$sqlRowCount = $sqlResult->num_rows;
}
else
{
$sqlRowCount = 0;
}