php 致命错误:在非对象上调用成员函数 rowCount()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27117824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fatal error: Call to a member function rowCount() on a non-object
提问by Jo'vani7
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:
我在我的登录中使用 PDO(如之前通过 sqli 所指示的),并且我已经尝试了以下操作,但是我遇到了这个致命错误,并且无法弄清楚应该给它什么,所以它满足了错误:
if($query->rowCount() > 0)
{
// session stuff
// refresh page
}
Then I tried this:
然后我尝试了这个:
if($query->rowCount() == 1)
{
// session stuff
// refresh page
}
Yet I still get this: Fatal error: Call to a member function rowCount() on a non-object
但我仍然得到这个:致命错误:在非对象上调用成员函数 rowCount()
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.
任何人都可以指出我在这里出错的地方。这是我抛出的唯一错误。
回答by stwalkerster
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.
您似乎正在$query
使用来自 的布尔返回值进行覆盖execute()
,从而为您留下一个非对象值(布尔值),您正试图在该值上调用方法。
Try something like this:
尝试这样的事情:
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();
// ...
}
回答by Whyle
$query = $dbc->prepare("SELECT * FROM members WHERE username = ? OR email = ?");
$query->execute(array($username, $email)):;
$count = $query->rowCount();
echo "Value is " . $count;
Try this.
尝试这个。