错误消息“在非对象上调用成员函数查询()”

时间:2012-07-04 01:04:00

标签: php mysql

在下面的代码中,我试图通过计算用户名来检查输入的用户名是否已被使用。

<?php
$hostname = 'aaa';
$username = 'bbb';
$password = 'ccc';
$prefix = "";
$dbna = 'ddd';
$username11 = $_POST['username'];
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$dbna", $username, $password);
$nRows = $pdo->query("SELECT count(Username) FROM Users WHERE Username ='$username11'")->fetchColumn(); 
if($nRows)
{
echo '<span style="color: red;">The username <b>'.$username11.'</b> is already in use.    </span>';
}
else
{
echo 'OK';
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>

错误是:

Fatal error: Call to a member function query() on a non-object in /home/check.php on line 10

我找到了这个错误的解决方案,但我找不到它。

1 个答案:

答案 0 :(得分:2)

$pdo->query

应该是:

$dbh->query