php 试图获取非对象的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5891911/
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 in
提问by Gereltod
on Control page:
在控制页面上:
<?php
include 'pages/db.php';
$results = mysql_query("SELECT * FROM sidemenu WHERE `menu_id`='".$menu."' ORDER BY `id` ASC LIMIT 1", $con);
$sidemenus = mysql_fetch_object($results);
?>
on View Page:
在查看页面上:
<?php foreach ($sidemenus as $sidemenu): ?>
<?php echo $sidemenu->mname."<br />";?>
<?php endforeach; ?>
Error is:
错误是:
Notice: Trying to get property of non-object in C:\wamp\www\phone\pages\init.php on line 22
注意:尝试在第 22 行获取 C:\wamp\www\phone\pages\init.php 中非对象的属性
Can you fix it? I don't have any idea what happened.
你能修好它吗?我不知道发生了什么。
采纳答案by Phil
Check the manual for mysql_fetch_object(). It returns an object, not an array of objects.
检查手册mysql_fetch_object()。它返回一个对象,而不是一个对象数组。
I'm guessing you want something like this
我猜你想要这样的东西
$results = mysql_query("SELECT * FROM sidemenu WHERE `menu_id`='".$menu."' ORDER BY `id` ASC LIMIT 1", $con);
$sidemenus = array();
while ($sidemenu = mysql_fetch_object($results)) {
$sidemenus[] = $sidemenu;
}
Might I suggest you have a look at PDO. PDOStatement::fetchAll(PDO::FETCH_OBJ)does what you assumed mysql_fetch_object()to do
我可以建议你看看 PDO。PDOStatement::fetchAll(PDO::FETCH_OBJ)做你认为mysql_fetch_object()要做的事
回答by alex
Your error
你的错误
Notice: Trying to get property of non-object in C:\wamp\www\phone\pages\init.php on line 22
注意:尝试在第 22 行获取 C:\wamp\www\phone\pages\init.php 中非对象的属性
Your comment
你的评论
@22 is
<?php echo $sidemenu->mname."<br />";?>
@22 是
<?php echo $sidemenu->mname."<br />";?>
$sidemenuis not an object, and you are trying to access one of its properties.
$sidemenu不是对象,而您正试图访问其属性之一。
That is the reason for your error.
这就是你的错误的原因。
回答by Jamie
<?php foreach ($sidemenus->mname as $sidemenu): ?>
<?php echo $sidemenu ."<br />";?>
or
或者
$sidemenus = mysql_fetch_array($results);
then
然后
<?php echo $sidemenu['mname']."<br />";?>
回答by james
$sidemenuis not an object, so you can't call methods on it. It is probably not being sent to your view, or $sidemenusis empty.
$sidemenu不是object,所以你不能调用它的方法。它可能没有发送到您的view,或者$sidemenus是空的。

