“致命错误:在非对象上调用成员函数fetch_assoc()"使用PDO时 [英] "Fatal error: Call to a member function fetch_assoc() on a non-object" when using PDO

查看:94
本文介绍了“致命错误:在非对象上调用成员函数fetch_assoc()"使用PDO时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决以下错误?

致命错误:在非对象上调用成员函数fetch_assoc() 在第27行的index.php中调用堆栈:0.0000 644064 1. {main}() index.php:0

Fatal error: Call to a member function fetch_assoc() on a non-object in index.php on line 27 Call Stack: 0.0000 644064 1. {main}() index.php:0

这是我到目前为止正在运行的代码:

This is the code I'm running so far:

$sql = 'SELECT * FROM pm_user WHERE name='.$_SESSION['pmname'].'';
$result = $pdo->query($sql);
$row = $result->fetch_assoc(); // line 27
echo $row['img'];

预先感谢

推荐答案

问题是:

  • 您正在对查询中的值进行插值. 这是错误和错误.您应该改用PDO的准备好的语句功能.

  • You are interpolating a value inside of your query. This is bad and wrong. You should use PDO's prepared statements feature instead.

在执行这种错误和错误插值的过程中,您忘记添加字符串引号,从而使您的SQL查询无效.

In the process of doing this bad and wrong interpolation, you forgot to add string quotations, making your SQL query invalid.

您没有对->query()进行任何错误检查 ;当query()失败时,它返回FALSE,因此您尝试对FALSE执行->fetch().

You failed to do any error checking whatsoever on ->query(); when query() fails, it returns FALSE and thus you tried to perform ->fetch() on FALSE.

修复所有这些问题.

这篇关于“致命错误:在非对象上调用成员函数fetch_assoc()"使用PDO时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆