PHP PDO:调用成员函数prepare() &列表?
致命错误:在非对象上调用成员函数prepare()
当我尝试执行以下操作时,我得到的结果是:
$sql = $connect->prepare ("SELECT id, pwd, firstname, lastname, approved, user_level FROM users WHERE $user_cond AND banned = 0"); // SELECT
$sql->execute ();
$sql->setFetchMode(PDO::FETCH_ASSOC);
$num = $connect->rowCount();
我坚持重写我的系统,取出所有 mysql_* 并改用 pdo。
这就是以前的情况:
$result = mysql_query("SELECT `id`,`pwd`,`firstname`,`lastname`,`approved`,`user_level` FROM users WHERE
$user_cond
AND `banned` = '0'
") or die (mysql_error());
$num = mysql_num_rows($result);
我做错了什么?
对于 list() 我有:
list($id,$pwd,$firstname,$lastname,$approved,$user_level) = mysql_fetch_row($result);
而不是 mysql_fetch_row($result);我应该在 PDO 中做什么?
我的 PDO 对象/连接:
try{
$connect = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset:UTF-8", DB_USER, DB_PASS, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch(PDOException $pe)
{
die('Could connect to the database because: ' .$pe->getMessage());
}
Fatal error: Call to a member function prepare() on a non-object
Is what I am getting when I try to do:
$sql = $connect->prepare ("SELECT id, pwd, firstname, lastname, approved, user_level FROM users WHERE $user_cond AND banned = 0"); // SELECT
$sql->execute ();
$sql->setFetchMode(PDO::FETCH_ASSOC);
$num = $connect->rowCount();
Im holding on rewriting my system, taking out all mysql_* and make use of pdo instead.
This is what was before:
$result = mysql_query("SELECT `id`,`pwd`,`firstname`,`lastname`,`approved`,`user_level` FROM users WHERE
$user_cond
AND `banned` = '0'
") or die (mysql_error());
$num = mysql_num_rows($result);
What have I done wrong?
And with list() I have:
list($id,$pwd,$firstname,$lastname,$approved,$user_level) = mysql_fetch_row($result);
instead of mysql_fetch_row($result); what should I do in PDO?
My PDO object/connection:
try{
$connect = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset:UTF-8", DB_USER, DB_PASS, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch(PDOException $pe)
{
die('Could connect to the database because: ' .$pe->getMessage());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通常意味着某些事情失败了并且你的变量不是你期望的那样。
$connection
可能是false
。PDOStatement::fetch
同时调用
rowCount
声明,而不是连接:usually means that something failed and your variable is not what you expect it to be.
$connection
is probablyfalse
.PDOStatement::fetch
Also call
rowCount
on the statement, not on the connection:由于您使用准备,您应该将 $cond 设为命名参数,然后执行以下操作:
在此示例中,条件只是 user_id,但您可以有多个条件和多个命名参数,只需以相同的方式对每个条件进行 bind()
$a 将是您的关联数组, print_r($a) 查看其中的内容
Since you using prepare, you should make the $cond a named parameter and then do this:
in this example the condition is just user_id, but you can have multiple conditions and multiple named parameters, just bind() each one the same way
The $a will be your assiciate array, print_r($a) to see what's in it
看起来像是范围问题。
您是在函数、类、命名空间中分配
$connect
还是在函数、类或命名空间中运行准备?如果您在函数中分配
$connect
,那么该函数应该返回 PDO 连接。我会像这样引入一个静态实例:然后像这样使用:
Looks like a scope issue.
Are you assigning
$connect
within a function, class, namespace or are you running prepare in a function, class or namespace ?if your assigning
$connect
within a function then that function should return the PDO Connection. I would introduce a static instance like so:then use like so:
该错误很可能是由于 MySQL 连接失败 - 因此
$connect
不是有效的 PDO 对象 - 因此,正如错误所述,您正在尝试调用一个对象上的方法,但该对象不支持它。您的连接处理代码是什么样的?您检查连接是否已成功创建?The error is most likely due to the MySQL connection failing - therefor
$connect
is not a valid PDO object - so as the error says, you're trying to call a method on an object which doesn't support it. What does your connection handling code look like? Did you check if a connection was successfully created?