[Solved] Error when running SQL syntax


The error:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given

is almost always because you’ve tried to execute a query and it’s failed for some reason, but you’ve continued on blindly anyway.

Upon failure, mysqli_query will return false rather than a mysqli_result and, if you then attempt to use that boolean false value in something like mysqli_num_rows, that’s exactly the error you’ll see.

As a first point, you should always check the return value of functions that can fail, that’s just good practice. Continuing blindly and hoping for the best is not really a way to produce robust code.


Having said that, you then have to move on to why the query has failed. A large proportion of SQL problems become obvious if you just print out the query before trying to execute it. It looks like you’ve eventually done this since one of your comments states that your query is:

SELECT * FROM Workhours WHERE AFNumber="AF1475" AND ( NOT LIKE '')

This is not a valid SQL statement, hence the problem. Now, despite the fact it appears to be using a different table name (an earlier edit of your question did have a matching table name), it corresponds closely to your $sql3 variable1:

$sql3 = "SELECT * FROM Work WHERE ID='".$_GET["id"]."' AND (".$row['Field']." NOT LIKE '".$_POST[$tempname]."')";

Hence it appears that neither $row['Field'] nor $_POST[$tempname] are actually set to anything.

That’s where you need to concentrate your effort, to find out exactly why they aren’t set.

For a start, it seems rather unwise to be using $row[anything] after the loop processing the rows has finished. It may well be that you need to move that code to within the loop to get it working but, without more information and context, that’s really just conjecture (though educated conjecture).


1 If it’s not the right query (I’ve just noticed it also uses ID rather than AFNumber), then the problem lies elsewhere other than the code you’ve shown. However, the method used to find the problem will remain the same: find the problematic query, print it out before execution, then work out why it’s malformed.

1

solved Error when running SQL syntax