PDO - CountRow - 错误::非对象

时间:2014-10-24 21:01:12

标签: php pdo

我有点问题。

$query = $connection->prepare("SELECT * FROM users WHERE username = ? OR mail = ?");
$query = $query->execute(array($username, $mail));
echo $query;
$rows = $query->fetchALL(PDO::FETCH_COLUMN, 0);
$numrows = count($rows);

我明白了:致命错误:在非对象上调用成员函数fetchALL()! 请帮忙。

EDIT1:

现在正在运行,但SQL没有执行。

$query = $connection->prepare("SELECT * FROM users WHERE username = ? OR mail = ?");
$result = $query->execute(array($username, $mail));
$count = $query->rowCount();
if ($count == 0) {
//
$query = $connection->prepare("INSERT INTO users(username,password,mail) VALUES (?,?,?)");
$query = $query->execute(array($username, $password, $mail));
//somewhere is mistake :( 

EDIT2:

一切都好...... 我在db中的结构是用户名,密码和电子邮件而不是邮件:)

3 个答案:

答案 0 :(得分:0)

您正在重命名$ query。 更改$ query = $ query->执行(...到$ execute = $ query->执行(...

然后$ rows = $ query-> fetchAll(...

答案 1 :(得分:0)

尝试以下代码

$query = $connection->prepare("SELECT * FROM users WHERE username = ? OR mail = ?");
         $query->execute(array($username, $mail));

         $count = $query->rowCount();
         $rows = $query->fetchALL(PDO::FETCH_COLUMN, 0);

答案 2 :(得分:0)

您正在重新分配$ query变量 尝试

    $query = $connection->prepare("SELECT * FROM users WHERE username = ? OR email = ?");
    $result = $query->execute(array($username, $mail));
    $rows = $result->fetchALL(PDO::FETCH_COLUMN, 0);
    $numrows = count($rows);

如有其他问题,请尝试

   $query = $connection->prepare("SELECT * FROM users WHERE username = ? OR mail = ?");
   $result = $query->execute(array($username, $mail));
   $count = $query->rowCount();
   if ($count == 0) {

    $query = $connection->prepare("INSERT INTO users(username,password,email) VALUES             (:username, :password, :mail)";)
    $query->execute(array(':username'=> $username,
                             ':password'=> $password,
                              ':email'= $mail));

 }