【问题标题】:Trying to get property of non-object when used on object在对象上使用时尝试获取非对象的属性
【发布时间】:2016-05-02 06:34:45
【问题描述】:

我有一些 PHP 将对象返回给变量:

                $db = new Db(); 
                $db->db_connect(); 
                $testSelectQuery = $db->db_select('SELECT * FROM test');
                //$dbValue = $testSelectQuery[0]->testcol;
                $test = $testSelectQuery[0];
                FB::log($test);

这是 $test 的输出:

Object {idtest: "1", testcol: "from db"}

现在,当我尝试回显其中一列时:

echo $test->testcol

我收到一个错误:

Notice: Trying to get property of non-object

它只是在输出中定义为一个对象。我做错了什么,我该如何解决?

编辑:vardump($test) 结果:array(2) { ["idtest"]=> string(1) "1" ["testcol"]=> string(7) "from db" }

【问题讨论】:

  • 您究竟在哪里尝试回显属性?我没有看到代码。
  • 紧随 FB::log($test);
  • FB::log 是做什么的?
  • @DavidTunnell 你试过了吗$test['testcol']
  • $test 不是一个对象。这是一个数组。

标签: php


【解决方案1】:

看起来$test 是一个关联数组

像这样访问它:

echo $test['testcol'];

【讨论】:

    猜你喜欢
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 2020-05-11
    • 2015-09-30
    相关资源
    最近更新 更多