Jump to content

Uncaught Error: Call to a member function rowCount()


ronc0011

Recommended Posts

I don’t seem to be able to find the issue with this code.
I’m following along with a tutorial I found on Youtube and so far I’ve managed to get everything to work . This piece is toward the end of this particular section and it’s giving me this error and I’ve looked and looked and don’t seem to be able to find the problem

 

I have a file named conn.php and of course a file, index.php.
The connection string is in “conn.php” like so…

<?php
$db_host = "127.0.0.1";
$db_user = "root";
$db_pw = "r00t";
$db = "test1";
echo "$db_host <br/> $db_user <br/> $db_pw <br/> $db <br/> ";

$conn = new PDO ( 'mysql:host = $db_host; dbname = $db; charset = utf8', "$db_user", "$db_pw");
var_dump ($conn);
?>

The extra stuff is just there to check the connection. Gives me positive feedback.

On the index.php page I begin the page with…

<?php
     require_once ("conn.php");
?>

Then after the Html DOCTYPE etc…  Inside the Body tags I have this…

<?php
  
      $query = "SELECT * FROM t1" ;
      $result = $conn -> query ($query);
      if ($result->rowCount()>0) {
          foreach ( $result as $item ) {
                  echo ( $item ['name']. $item ['email']);
      
       }     
       } 
  ?>

There is a database named test1 with a table named t1. In this table there are 3 columns ID, name, and email. With 4 records / rows.

 

When I try to run this it throws this error…

 

Fatal error: Uncaught Error: Call to a member function rowCount() on boolean in C:\Apache24\htdocs\t-1\index.php:33 Stack trace: #0 {main} thrown in C:\Apache24\htdocs\t-1\index.php on line 33

 

I’m running all this on my laptop I have Apache 2.4 and PHP 7 and MySQL 5.6. These are all straight installs. No WAMP .

Am I just not seeing something really obvious???
Any thoughts on this???

Link to comment
Share on other sites

The problem is that $result is not an object - it is a Boolean (i.e. True/False). The error you are getting is due to the fact that you are trying to invoke a method 'rowCount' on that Boolean as opposed to invoking it on an Object that supports that method.

 

So, the next question is "why" is that a Boolean and not a query result object. The answer lies on this line:

 

$result = $conn -> query ($query);

 

You can't put spaces around the object operator ( -> ). I'm surprised you are not getting errors. Try this

 

$result = $conn->query($query);
Link to comment
Share on other sites

You can't put spaces around the object operator ( -> ). I'm surprised you are not getting errors

Actually that's fine. Whitespace is allowed anywhere except in the middle of a token.

 

I'm thinking

'mysql:host = $db_host; dbname = $db; charset = utf8'
Link to comment
Share on other sites

Thank you for your quick and informative response.
I made the corrections you noted . In fact when that didn’t seem to work I copy / pasted it into my page. Still no joy. So now that block reads thus…

  <?php
  
      $query = "SELECT * FROM t1" ;
      $result = $conn->query($query);
      if ($result->rowCount()>0) {
         foreach ( $result as $item ) {
          echo ( $item ['name']. $item ['email']);
      
       }     
       } 
  ?>

Still getting the same error. Actually I had suspected the line you referenced but couldn’t spot the problem. However I wasn’t aware of the “spaces” issue” (I’m very new to PHP). In any event the issue persist. So clearly, the error message says it’s trying to call a function on a Boolean  but what I’m not understanding is why the Youtube presenter’s code is working and mine isn’t. Why isn’t his code seeing that variable as a Boolean and choking when it tries to run? The only difference I can see is that the Youtube code names his connection string $odb and I named mine $conn. BTW the video is here 

 

https://www.youtube.com/watch?v=BQx5TJe1Fr0

 

And that block of code is in the last half of the video.

Link to comment
Share on other sites

Hmm..  I’m not real sure what the fifferencwe was but I’ll review is later I actuall had a previous version on the file commented out which I just swapped for the one that wasn't working and apparently that was the problem, made the swap and every hing started working
So here’s the working version…

$conn = new PDO ( "mysql:host=" . $db_host . ";dbname=" . $db , $db_user , $db_pw);
var_dump ($conn);
Link to comment
Share on other sites

your connection string is in single-quotes. this would result in the php variables NOT being replaced with their value.

 

as to why this isn't throwing an exception from the connection code, i have seen cases where the DSN has been wrong/gibberish but with no error. in fact, just tested, the OP's connection code, with a correct username/password, results in the following from the var_dump() -

 

object(PDO)#1 (0) { }

 

 

if the OP had enabled exceptions in the connection code, there would be a 'no database selected' error at the ->query() statement.

 

change the single-quotes to double-quotes in the 1st parameter in the connection code.

 

edit: and you will need to remove the spaces as Barand has indicated above.

Link to comment
Share on other sites

your connection string is in single-quotes. this would result in the php variables NOT being replaced with their value.

 

as to why this isn't throwing an exception from the connection code, i have seen cases where the DSN has been wrong/gibberish but with no error. in fact, just tested, the OP's connection code, with a correct username/password, results in the following from the var_dump() -

 

 

if the OP had enabled exceptions in the connection code, there would be a 'no database selected' error at the ->query() statement.

 

change the single-quotes to double-quotes in the 1st parameter in the connection code.

 

edit: and you will need to remove the spaces as Barand has indicated above.

 

 

This apparently was the issue It's not the first time I've been tripped up by the double / single quotes issue. In fact I did have that string as double quotes originally and swapped them for single quotes when it wasn't working . I don't remember why / how it wasn't working. That was 4 days ago.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.