Thursday, November 17, 2016

mysqli_query() expects parameter 1 to be mysqli_result

mysqli_query() expects parameter 1 to be mysqli_result

See the above message. Then,

Study the script below carefully. There is an error in it. At first glance, it may not be so obvious, especially if you are new to database handling.

$updateQry ="update TABLE set approved=$value where id=$id";
$updQryResult =mysqli_query($conn, $updateQry);
if (!($updQryResult)) {
   $updateResult =0;
else {
   $updateResult =mysqli_affected_rows($updQryResult);

if ($updateResult) {
   echo 'Rejected';
else {
   echo "Update failure";

Okay, the error is in line 7. Yes, easy wasn't it? It is always easy when we know the answer.

How did I miss it?

Let me explain for those who did not see it.

The correct statement in line 7 of the above code should have been
$updateResult =mysqli_affected_rows($conn);

Most of us commit this mistake, at some point or other of the learning curve in MySQL and PHP.

By way of habit, we pass the result set handle to the function mysqli_affected_rows(), which will result in a cryptic message, "mysqli_query() expects parameter 1 to be mysqli_result"

You need to present the database handle as the parameter for mysqli_affected_rows(), mysqli_num_rows() etc.,

Post a Comment

Enforcing Helmet Rule in Chennai!!! Misplaced Priorities?!

News : Helmets are mandatory in Chennai, for even the pillion rider, rules the  High Court, apparently to save the precious lives of its c... Registered & Protected DWYE-NHTO-NBNH-7FFM