我是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;
答案 0 :(得分:1)
如果查询返回空结果集,您仍然试图访问该行。访问while循环中的db列,如下所示。
while ($row = $result->fetch_object()) {
echo $row->teamTitle;
}
答案 1 :(得分:1)