【问题标题】:PHP throwing invalid PHP Notice: Trying to get property of non-objectPHP throwing invalid PHP Notice: Trying to get property of non-object
【发布时间】:2013-07-20 21:02:40
【问题描述】:

我收到一个似乎发生错误的错误。

我已经在不同的类中获得了足够多的相同功能,但似乎没有发生。 我正在使用的功能是:

public function UserInfo($type, $value) {
    if($type == 'email') {
       $query = $this->db->prepare("SELECT * FROM `accounts` where `provider` = '1' AND `email` = :value AND `type` = 'client' LIMIT 1");
    } else {
       $query = $this->db->prepare("SELECT * FROM `accounts` where `provider` = '2 'AND `prov_id` = :value AND `type` = 'client' LIMIT 1");
    }

    $params = array(":value" => $value,);
    $query->execute($params);
    return $query->FetchObject();
}

我正在尝试通过以下方式获取数据:

$clients->UserInfo("id", $uid)->email;

PHP 返回值,因此很明显该对象确实存在,但它仍然抛出

PHP Notice: Trying to get property of non-object in /Users/Luke/public_html/manage.php on line 30

我使用的语法有问题,还是 PHP 错误?

【问题讨论】:

  • 两者都不是。 2 个对象之一根本不存在
  • 如果 PHP 告诉你某物不是对象,那么它就不是。找出它是什么并解决问题。
  • do var_dump($clients->UserInfo("id", $uid)) 将显示它是否为对象
  • 向我们展示它返回的内容
  • 第 30 行是哪一行?

标签: php object pdo


【解决方案1】:

试试这个。
这不会导致任何错误。

<?php

class DB {

    protected $db;

    public function __construct() {
        $this->db = PDO(
            'mysql:dbname=test;host=127.0.0.1;charset=utf8',
            'root', 
            ''
        );
        $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }

    public function UserInfo($type, $value) {
        if ($type == 'email') {
            $stmt = $this->db->prepare(
                "SELECT * FROM `accounts` where `provider` = '1' AND `email` = ? AND `type` = 'client' LIMIT 1"
            );
        } else {
            $stmt = $this->db->prepare(
                "SELECT * FROM `accounts` where `provider` = '2 'AND `prov_id` = ? AND `type` = 'client' LIMIT 1"
            );
        }
        $stmt->execute(array($value));
        $result = $stmt->fetchObject();
        if ($result === false) {
            throw new Exception('value not found');
        }
        return $result;
    }

}

try {

    $clients = new DB;
    echo $clients->UserInfo("id", $uid)->email;

} catch (Exception $e) {

    echo $e->getMessage();

}

【讨论】:

    【解决方案2】:

    我认为 PHP 在抱怨这一行:

    $query->execute($params);
    

    因为$query 没有在 if else 子句之外声明。另一种可能性是查询错误,所以$queryprepare() 之后是Falsehttp://php.net/manual/en/pdo.prepare.php

    你可以试试:

    public function UserInfo($type, $value) {
        $sql_query = '';
        if($type == 'email') {
           $sql_query = "SELECT * FROM `accounts` where `provider` = '1' AND `email` = :value AND `type` = 'client' LIMIT 1";
        } else {
           $sql_query = "SELECT * FROM `accounts` where `provider` = '2 'AND `prov_id` = :value AND `type` = 'client' LIMIT 1";
        }
    
        $query = $this->db->prepare($sql_query);
        $params = array(":value" => $value,);
    
        if ($query === false) return 'False query';
    
        $query->execute($params);
        return $query->FetchObject();
    }
    

    【讨论】:

    • 实际上在 PHP 中 if 的作用域并不特殊,所以它不是作用域问题。另外,为什么要返回字符串而不是布尔值?
    • 如果您打开所有错误并通知登录 PHP,如果其他问题,它将抱怨。返回字符串只是@LukeT 的一个demo,让他有基本的想法,他可以返回任何他喜欢的东西。
    猜你喜欢
    • 2019-11-05
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 2021-01-13
    • 2023-02-24
    相关资源
    最近更新 更多