如何修复错误:尝试获取非对象的属性?
我想在点击 $_POST['name']
后获取,并在名称插入的位置获取行 ID 以插入其他表中,但出现以下错误。错误的原因是什么?如何修复?
$name = $this -> input -> post('name');
$query_re = $this->db->get_where('tour_foreign', array('name' => $name))->row();
$data2 = array();
foreach ($residence_name as $idx => $name) {
$data2[] = array(
'relation' => $query_re->id, //This is line number 58
'hotel_id' => $residence_id[$idx],
'name' => $residence_name[$idx],
);
};
$data222 = $this->db->insert_batch('residence', $data2);
错误:
遇到 PHP 错误
严重性:通知
消息:正在尝试 获取非对象的属性
文件名:foreign.php
Line 数量:58
I want get from after click $_POST['name']
and where it with name inserting that get row id for insert in other table, but get following error. What is the cause of the error? How to fix?
$name = $this -> input -> post('name');
$query_re = $this->db->get_where('tour_foreign', array('name' => $name))->row();
$data2 = array();
foreach ($residence_name as $idx => $name) {
$data2[] = array(
'relation' => $query_re->id, //This is line number 58
'hotel_id' => $residence_id[$idx],
'name' => $residence_name[$idx],
);
};
$data222 = $this->db->insert_batch('residence', $data2);
Error:
A PHP Error was encountered
Severity: Notice
Message: Trying
to get property of non-object
Filename: foreign.php
Line
Number: 58
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看来这个数据库调用返回零行:
在调用 row() 之前还可以考虑使用 num_rows() 检查数据库调用的结果。
It seems that this database call returns zero rows :
Also consider using num_rows() to check the result of the database call before calling row().
基本上,代码试图告诉您的是,您正在尝试获取不是对象(类的实例)的变量的属性。
所以像数字 58 一样,它表示 $query_re 不是一个对象,因此不能具有属性“id”。
Basically what the code is trying to tell you, is that you're trying to get a property of a variable which is NOT an object (an instance of a class).
So on like number 58, it's saying that $query_re is not an object, and therefore cannot have the property 'id'.
PHP 不允许您将函数的返回值视为对象,除非它返回引用。尝试
编辑:
没关系,我错过了“第 58 行”注释,我猜 PHP5 允许链接。想要
PHP doesn't allow you to treat the return value of a function as an object, unless it returns a reference. Try
Edit:
Nevermind, I missed the 'line 58' comment, and I guess PHP5 allows chaining. Fancy