Fatal error: Call to a Member Function rowCount() on Boolean

Asked

Viewed 1,850 times

0

Hello,

I’m a beginner in PDO connections in PHP and my first select I am facing the following error:

Fatal error: Call to a member function rowCount() on boolean in C:\MAMP\htdocs\samples\pdo\conexao.php on line 12

Follows my code:

<?php
    try {
        $pdo = new PDO("mysql:host=localhost;dname=dashboard","root","root");
    } catch(PDOException $e) {
        echo '<b style="color:red">'.$e->getMessage().'</b>';
    }

    $buscarUsuario = $pdo->query("SELECT * FROM usuarios");

    $buscarUsuario->rowCount(); //linha 12

I checked the connection and also the table name, there is nothing wrong, I looked for possible solutions to this my problem here on the forum and nothing that solved, and when giving var_dump users, it returns me bool(false).

  • If by chance the query is not executed, $pdo->query() returns a boolean == false

  • Add $buscarUsuario = $pdo->query("SELECT * FROM usuarios"); within the try { and make if($buscarUsuario){ $buscarUsuario->rowCount(); }

  • @Marceloboni but query not running??

  • If it had been executed, it would not return bool(false), do what I recommended and see which error was given in the query

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.