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 ...