Jump to content

mysqli query returning nothing


SamohtVII
 Share

Recommended Posts

So I just converted my mysql to mysqli and I noticed when I do something like this:

$sql = $mysqli->query("SELECT * FROM `history` WHERE user = '$user'");	while($row = $sql->fetch_assoc())	{         //DO SOMETHING                } ?>

If the query finds nothing it gets an error so now I am prefixing all my while statements with if($sql).

If mysqli is so much better why does this error happen and is there a better way for me to avoid adding that extra if everytime.

 

Thanks

Link to comment
Share on other sites

What's the error? BTW, don't think you need the table name in quotes.

 

Also, use mysqli_error

www.w3schools.com/php/func_mysqli_error.asp

Edited by niche
Link to comment
Share on other sites

If mysqli is so much better why does this error happen

The mysqli extension will not automatically make you a better programmer, sorry. Just because you didn't bother to do any error checking when you were using the mysql extension doesn't mean that you should go forward and blindly assume that things are working without bothering to check. Look in the documentation for information about checking for errors in queries, checking how many records were returned, etc.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...