1

I know this has been asked before but I've spent time going through the responses and can't find anything that points me in the right direction.

I have the following code:

<?php
$connSel = OpenCon();
$sqlSel = 'SELECT * FROM sitecomments';
$stmtSel = $connSel -> prepare($sqlSel);
$q_resultSel = $stmtSel -> execute();

if(sizeof($q_resultSel)>0){
    while($row = $q_resultSel->fetch_assoc()){
        echo $row['username']."<br><br>".$row['comment'];
    }
}
else{
    echo '<i>No comments yet</i>';
}
?>

I have checked the query in the database and it returns everything fine. However, it's not working here. I get the error message "Call to a member function fetch_assoc() on boolean".

I have tried to trace the error and it seems to be that the line

while($row = $q_resultSel->fetch_assoc())

is returning false as it does with a query fail and so iterating it is not working. I tried to find the number of rows in $q_resultSel but get the message "Trying to get property of non-object" so I'm not sure I understand that particular error checking method I found in previous posts.

Any ideas would be much appreciated.

Dharman
  • 30,962
  • 25
  • 85
  • 135

4 Answers4

1

Change sizeof() to $stmtSel->num_rows

    $connSel = OpenCon();
    $sqlSel = 'SELECT * FROM sitecomments';


    if($stmtSel = $connSel -> prepare($sqlSel)){   
    $q_resultSel = $stmtSel -> execute(); 
    $stmtSel ->store_result();
    if($stmtSel->num_rows>0){      
      $result = $q_resultSel->get_result();
        while ($row = $result->fetch_array(MYSQLI_NUM)){
            echo $row['username']."<br><br>".$row['comment'];
        }
    }
}
    else{
        echo '<i>No comments yet</i>';
    }
Batsheva Hansav
  • 316
  • 2
  • 11
1

Regarding error checking I tried $q_resultSel = $stmtSel -> execute() or die(mysqli_error($q_resultSel)); but it output nothing

When you check errors, you should check after every prepare() and every execute(), because different kinds of errors occur in each case.

When you get an error on prepare(), it means creating the statement failed, so you have to get the error from the connection:

$stmtSel = $connSel -> prepare($sqlSel);
if ($stmtSel === false) {
    die($connSel->error);
}

When you get an error on execute(), it means you had a statement, but executing that statement failed. So you have to get the error from the statement:

$q_resultSel = $stmtSel -> execute();
if ($q_resultSel === false) {
    die($stmtSel->error);
}

An alternative is to enable mysqli exceptions, so you don't have to write any code to check explicitly for errors. If there are errors, they will interrupt your code execution by throwing an exception.

Here's how to enable exceptions:

$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_ALL;

Read the docs about error checking:


The more I look at your code, the more mistakes I notice.

$q_resultSel = $stmtSel -> execute();

You should read the docs on the execute() function:

Return Values

Returns TRUE on success or FALSE on failure.

Note that execute() does not return an object — it returns a boolean both when it is successful or when there's an error.

That means $q_resultSel->fetch_assoc() is bound to cause a fatal error even when there is no problem with the query. It's always invalid to use -> on a boolean value in PHP.

if(sizeof($q_resultSel)>0){

Checking the sizeof the result is redundant. Because the only thing you do with it if it's >0 is to run a while loop on it, you might as well skip the if condition, because if the result has 0 rows, then the while loop will just terminate immediately anyway.

while($row = $q_resultSel->fetch_assoc()){
    echo $row['username']."<br><br>".$row['comment'];
}

As mentioned earlier, you're trying to call a method of $q_resultSel, which is bound to be a boolean TRUE or FALSE. That's the cause of the fatal error.

Instead, you should get a result object after calling execute() after confirming that the execute() did not return FALSE indicating an error:

$ok = $stmtSel -> execute();
if ($ok === false) {
    die($stmtSel->error);
}

$q_resultSel = $stmtSel->get_result();
if ($q_resultSel === false) {
    die($stmtSel->error);
}

while($row = $q_resultSel->fetch_assoc()){
    echo $row['username']."<br><br>".$row['comment'];
}

Read the docs about get_result, with code examples:

Note that get_result() is a function you can use only if your PHP installation uses the mysqlnd driver. Most modern PHP installations should do that by default, but you can check by getting PHP config info at the shell command line:

php -i

... lots of output ...

mysqlnd

mysqlnd => enabled

... lots more output ...
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • Neither of the if statements are outputting anything. Enabling the exceptions didn't seem to do anything either. I'm still just left with Fatal error: Uncaught Error: Call to a member function fetch_assoc() on boolean in C:\inetpub\wwwroot\jw17acs\public\competencies\task4\aboutme.php:117 Stack trace: #0 {main} thrown in C:\inetpub\wwwroot\jw17acs\public\competencies\task4\aboutme.php on line 117 – Jane Semtner Mar 02 '19 at 19:56
0

$stmtSel -> execute() - returns a bool value. So you're trying to call a method fetch_assoc() on a boolean value, which is wrong.
Try to replace:

q_resultSel = $stmtSel -> execute();

on

$stmtSel -> execute();
q_resultSel = $stmtSel->get_result();
0

I think problem is here.

if(sizeof($q_resultSel)>0)

replace sizeof to count and check it.

if(count($q_resultSel)>0)
Hasee Amarathunga
  • 1,901
  • 12
  • 17