如何修复错误:尝试获取非对象的属性?

发布于 2024-12-10 06:50:18 字数 710 浏览 0 评论 0原文

我想在点击 $_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月亮坠入山谷 2024-12-17 06:50:18

看来这个数据库调用返回零行:

$query_re = $this->db->get_where('tour_foreign', array('name' => $name))->row();

在调用 row() 之前还可以考虑使用 num_rows() 检查数据库调用的结果。

It seems that this database call returns zero rows :

$query_re = $this->db->get_where('tour_foreign', array('name' => $name))->row();

Also consider using num_rows() to check the result of the database call before calling row().

逐鹿 2024-12-17 06:50:18

基本上,代码试图告诉您的是,您正在尝试获取不是对象(类的实例)的变量的属性。

所以像数字 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'.

撧情箌佬 2024-12-17 06:50:18
$query_re = $this->db->get_where('tour_foreign', array('name' => $name))->row();
if(is_object($query_re))  {
    ....
}
$query_re = $this->db->get_where('tour_foreign', array('name' => $name))->row();
if(is_object($query_re))  {
    ....
}
南薇 2024-12-17 06:50:18

PHP 不允许您将函数的返回值视为对象,除非它返回引用。尝试

$where = $this->db->get_where(...);
$query_re = $where->row();

编辑:

没关系,我错过了“第 58 行”注释,我猜 PHP5 允许链接。想要

PHP doesn't allow you to treat the return value of a function as an object, unless it returns a reference. Try

$where = $this->db->get_where(...);
$query_re = $where->row();

Edit:

Nevermind, I missed the 'line 58' comment, and I guess PHP5 allows chaining. Fancy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文