When calling multiple stored procedures, you can run into the following error: "Commands out of sync; you can't run this command now".This can happen even when using the close() function on the result object between calls. To fix the problem, remember to call the next_result() function on the mysqli object after each stored procedure call. See example below:

<?php// New Connection$db = new mysqli('localhost','user','pass','database');

The cryptic "Couldn't fetch mysqli" error message can mean any number of things, including:

1. You're trying to use a database object that you've already closed (as noted by ceo at l-i-e dot com). Reopen your database connection, or find the call to <?php mysqli_close($db); ?> or <?php $db->close(); ?> and remove it.2. Your MySQLi object has been serialized and unserialized for some reason. Define a wakeup function to re-create your database connection. http://php.net/__wakeup3. Something besides you closed your mysqli connection (in particular, see http://bugs.php.net/bug.php?id=33772)4. You mixed OOP and functional calls to the database object. (So, you have <?php $db->query() ?> in the same program as <?php mysqli_query($db) ?>).

For those using with replication enabled on their servers, add a mysqli_select_db() statement before any data modification queries. MySQL replication does not handle statements with db.table the same and will not replicate to the slaves if a scheme is not selected before.

This may or may not be obvious to people but perhaps it will help someone.

When running joins in SQL you may encounter a problem if you are trying to pull two columns with the same name. mysqli returns the last in the query when called by name. So to get what you need you can use an alias.

Below I am trying to join a user id with a user role. in the first table (tbl_usr), role is a number and in the second is a text name (tbl_memrole is a lookup table). If I call them both as role I get the text as it is the last "role" in the query. If I use an alias then I get both as desired as shown below.

When building apps, i like to see the whole statement when if fails.<?php$q="SELECT somecolumn FROM sometable"; //some instruction$r=mysqli_query($DBlink,$q) or die(mysqli_error($DBlink)." Q=".$q);?>If theres an error (like my numerous typing mistakes) this shows the entire instruction.Good for development (not so good on production servers - simply find and replace when finished: $r=mysqli_query($DBlink,$q); )

If you use the default resultmode of MYSQLI_STORE_RESULT, you can call $mysqli->close() right after $mysqli->query and before you use mysqli_result. This reduces the time the connection is open, which can help if the database server has a limit on how many connections there can be.

Beeners' note/example will not work. Use mysqli_multi_query() to call a Stored Procedure. SP's have a second result-set which contains the status: 'OK' or 'ERR'. Using mysqli_query will not work, as there are multiple results.

WARNING: This function was buggy on 64bit machines till 5.0.5. Affected versions 5.0.0-5.0.4. The problem appears when a value for the third parameter is passed - this will lead to instant FALSE returned by the function. Therefore if you need to use unbuffered query don't use this function with the aforementioned versions but you mysqli_real_query() and mysqli_use_result().If you have the rights to patch you PHP installation the fix is easy:In file ext/mysqli/myslqi_nonapi.c, function PHP_FUNCTION(mysqli_query)changeunsigned int resultmode=0;tounsigned long resultmode=0;

I don't know is it bug or something , then first I write it here . Query SHOW with MYSQLI_USE_RESULT option don't show num_rows :<?phpSHOW TABLES LIKE [some table], MYSQLI_USE_RESULTnum_rows // shows 0 !?>

Recently I had puzzling problem when performing DML queries, update in particular, each time a update query is called and then there are some more queries to follow this error will show on the page and go in the error_log:"Fatal error: Exception thrown without a stack frame in Unknown on line 0"

The strange thing is that all queries go through just fine so it didn't make much sense:

In the above example "$update" is "true", mysqli_error() is empty and of course the update operation goes through, however the nasty super cryptic error appears on the page.What makes even less sense to me is how I fixed it - just called "mysqli_free_result" after the update query and the problem was gone, however because mysqli_free_result is not supposed to be called after DML queries (to free what, a boolean? lol) it needs to be wrapped in a try catch block:

try{ mysqli_free_result($update);}catch (Exception $e){ //do nothing}

So, I don't know why but it seems that when DML queries are responsible for:"Fatal error: Exception thrown without a stack frame in Unknown on line 0"calling "mysqli_free_result" after the query seems to be fixing the issue