试图获取非对象MySQLi结果的属性 [英] Trying to get property of non-object MySQLi result
问题描述
有一些我在苦苦挣扎的PHP代码-搜索了Google等,并尝试了提及的所有内容,但是由于某种原因,我无法解决它.
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.
问题是:
我有一些代码正在查询数据库中是否存在特定用户.
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;
}
...
?>
我已经var转储了查询结果($ r)并得到了它:
I've var dumped the result of the query ($r) and got this:
object(mysqli_result)#4 (5) { ["current_field"]=> int(0) ["field_count"]=> int(1) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) }
这是正确的,上面应该只有1行.
This is correct, there should only be 1 row as above.
我确实收到此错误,链接到显示if ($r->num_rows) {
I do get this error linking to the line saying if ($r->num_rows) {
注意:尝试在第LINE行的FILE中获取非对象的属性
Notice: Trying to get property of non-object in FILE on line LINE
但是我不知道为什么,因为对象是有效的(如上所述),并且应该可以正常工作.据我所知,似乎一切顺利,我只是想知道为什么会出现错误.我敢肯定这很简单,但我会很感激.
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.
推荐答案
$sql = "SELECT * FROM table";
$result = $conn->query($sql);
if (!$result) {
trigger_error('Invalid query: ' . $conn->error);
}
使用mysqli_error()函数检查错误
check the error with mysqli_error() function
您的查询可能有错误.
这篇关于试图获取非对象MySQLi结果的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!