Commands Out of Sync Error When Using Stored Procedures

Especially if you are using database sessions with CodeIgniter, you should be aware that if you call a stored procedure, any query after the call with give you the Commands Out of Sync error:

A Database Error Occurred
Error Number: 2014
Commands out of sync; you can’t run this command now
— The query after your procedure call —
Filename: libraries/Session/drivers/Session_database_driver.php
Line Number: 247

Reproduce the Error

To reproduce the error I just created a simple stored procedure that selects all the users in Community Auth’s users table:

CREATE PROCEDURE `get_all_users` ()
SELECT * FROM users;
END $$

Then, in my application I call it:

$query = $this->db->query('CALL get_all_users()');

When I request the page where this is run, I get the commands out of sync error.

The Solution

To fix this, I simply add the following line after the procedure call:

 mysqli_next_result( $this->db->conn_id );

The error goes away.