如何删除错误“试图获取非对象的属性”?

时间:2015-07-28 06:20:40

标签: php mysql

我是PHP新手。我运行代码Trying to get property of non-object时遇到错误消息我尝试stackoverflow上提供的所有可能的解决方案,但我失败了。请查看我的代码并建议我的错误。

请勿将其标记为重复

<?php
   $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "pacra-daily";
    $conn = new mysqli($servername, $username, $password, $dbname);
    //$id2 = $_GET['id'];

    $sql="SELECT pacra_teams.title as 'teamTitle', og_users.display_name, og_users.id
    FROM og_users
    LEFT JOIN pacra_teams
    ON pacra_teams.id = og_users.team_id
    Where og_users.id = 106";
    $result = $conn->query($sql);
    $row = $result->fetch_object();
    // setting variable values during session

    echo $row->display_name; // Change to correct column name in table og_users
    echo $row->teamTitle; 
    ?>

我的错误

echo $row->display_name; // Change to correct column name in table og_users
        echo $row->teamTitle; 

2 个答案:

答案 0 :(得分:1)

如果查询返回空结果集,您仍然试图访问该行。访问while循环中的db列,如下所示。

while ($row = $result->fetch_object()) {
      echo $row->teamTitle;
}

答案 1 :(得分:1)

检查你的任务......看一下php.net http://php.net/manual/de/mysqli.query.php

以下是您的示例

master_page.php