致命错误:在非对象上调用成员函数rowCount() [英] Fatal error: Call to a member function rowCount() on a non-object
问题描述
我在登录名中使用PDO(如先前在sqli上所指示的那样),并且我尝试了以下操作,但是却遇到了致命错误,并且无法弄清楚给出什么,因此它满足该错误:
I'm using PDO in my login (as instructed previously over sqli), and I have tried the following, but yet I am getting this Fatal Error, and cannot figure out what to give it, so it satisfies the error:
if($query->rowCount() > 0)
{
// session stuff
// refresh page
}
然后我尝试了这个:
if($query->rowCount() == 1)
{
// session stuff
// refresh page
}
但是我仍然得到这个消息:致命错误:在非对象上调用成员函数rowCount()
Yet I still get this: Fatal error: Call to a member function rowCount() on a non-object
这是我在进行更改之前开始的内容:
Here's is what I started with before the changes:
$count = $query->rowCount();
最后,这是一个更好的代码段,因此您可以了解其中涉及的内容:
Lastly, here's a better snippet so you can get an idea of what's involved:
<?php
include("/scripts/Connections.php");
$email = $_POST['email'];
$username = $_POST['username'];
$password = md5($_POST['password'], "DDerehOjhdfDDf$$##%^)-=_/.#$#dkfsj!`~efjkf(*)/)sD");
$confPassword = md5($_POST['conPassword'], "DDerehOjhdfDDf$$##%^)-=_/.#$#dkfsj!`~efjkf(*)/)sD");
if(isset($email, $username, $password, $confPassword)) {
if(strstr($email, "@")) {
if($password == $confPassword) {
$query = $dbc->prepare("SELECT * FROM members WHERE username = ? OR email = ?");
$query = $query->execute(array(
$username,
$email
));
$count = $query->rowCount();
if($count == 0) {
$query = $dbc->prepare("INSERT INTO memebers SET username = ?, email = ?, password = ?");
$query = $query->execute(array(
$username,
$email,
$password
));
if($query) {
echo "Your account has been registered, you may login!";
}
}
else {
echo "A user already exists with that username/password.";
}
}
else {
echo "Your passwords do not match!";
}
}
else {
echo "Invalid email address!";
}
}
?>
任何人都可以指出我在哪里出问题了.这是我抛出的唯一错误.
Can anyone point where I'm going wrong here. This is my only error this is being thrown.
推荐答案
您似乎正在用execute()
中的布尔返回值覆盖$query
,而给您留下了一个非对象值(布尔)尝试在其上调用方法.
You appear to be overwriting $query
with the boolean return value from execute()
, leaving you with a non-object value (boolean) which you're trying to call a method on.
尝试这样的事情:
if($password == $confPassword) {
$query = $dbc->prepare("SELECT * FROM members WHERE username = ? OR email = ?");
$result = $query->execute(array(
$username,
$email
));
// check the value of $result is true here - if not,
// your query has failed to execute and handle the error
// appropriately.
$count = $query->rowCount();
// ...
}
这篇关于致命错误:在非对象上调用成员函数rowCount()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!