Note: The code below will not work on all environments. I’m using php 5.2.6 with mysql driver 5.0.18.

In a previous post:

I explained how to use MySQL stored procedures with different db adapters such as mysql, mysqli, and pdo in PHP. In those examples, I demonstrated calling a sp with in/out variables and calling a stored procedure that returns a recordset. One of the comments I received was a person asking how to call a stored procedure that uses in/out parameters as well as returns a recordset. It’s not much different and here’s how.

The trick is to combine both methods in one. Here’s an example of what the stored procedure looks like:

DROP PROCEDURE IF EXISTS `test`.`get_users` $$
IN firstName VARCHAR(100),
OUT totalUsers INT
INTO totalUsers
FROM users
WHERE first_name = firstName;
FROM users
WHERE first_name = firstName;
END $$

Notice there are two statements in the body of this stored procedure. The first select count(*) statement counts the total number of people who’s first name is equal to the in variable firstName. Once it’s gets the count, it sets the out variable totalUsers to that value.

The next statement is a simple select. This will select all fields for users who’s first name is equal to the in variable firstName and return the recordset. So by calling this stored procedure and passing in two parameters (first name, total), a recordset will be returned and an out variable will be set – that can then be queried.

I’m using the same users table from my previous post on stored procedures. Click on the link above to access that post and copy the create statement. I’m using a bit more data on the users table this time for this example to work. Here’s the insert statement:

VALUES (NULL, ‘Joey’, ‘Rivera’), (NULL, ‘John’, ‘Doe’), (NULL, ‘Joey’, ‘Tester’),
(NULL, ‘Joey’, ‘Test’), (NULL, ‘Billy’, ‘Bob’);

The code is pretty simple, pretty much you want to do two queries. The first calls the stored procedure and passes in both variables. This first query will return a recordset (the second select statement in the body of the stored procedure. The next query will select totalUsers to get that variable and display it. Here’s the php code to do this in both mysql and pdo_mysql:


$mysql = mysql_connect(‘localhost’, ‘test’, ‘test’, false, 65536);
mysql_select_db(‘test’, $mysql);
$rs = mysql_query("call get_users(‘joey’, @total);");
while($row = (mysql_fetch_assoc($rs)))
echo ‘<br />’;
// now get the @total var
$rs2 = mysql_query("select @total;");
$total = mysql_fetch_assoc($rs2);


$pdo = new PDO(‘mysql:dbname=test;host=’, ‘test’, ‘test’);
$data = $pdo->query("call get_users(‘joey’, @totalUsers);")->fetchAll(PDO::FETCH_ASSOC);
$total_count = $pdo->query("select @totalUsers;")->fetchAll(PDO::FETCH_ASSOC);
echo ‘<br />’;

This code should output:


Array ( [users_id] => 6 [first_name] => Joey [last_name] => Rivera )
Array ( [users_id] => 8 [first_name] => Joey [last_name] => Tester )
Array ( [users_id] => 9 [first_name] => Joey [last_name] => Test )
Array ( [@total] => 3 )


Array ( [0] => Array ( [users_id] => 6 [first_name] => Joey [last_name] => Rivera ) [1] => Array ( [users_id] => 8 [first_name] => Joey [last_name] => Tester ) [2] => Array ( [users_id] => 9 [first_name] => Joey [last_name] => Test ) )
Array ( [0] => Array ( [@totalUsers] => 3 ) )

Like I mentioned before, both sets of code are doing the same thing, calling two queries. First the stored procedure (passing in joey for firstname and @total as the out var) then query for the out variable @total. I took a few shortcuts with pdo so it seems like less code. I personally like pdo better and it’s what I use on my projects. I hope you all find this helpful and feel free to ask any question or post any comments.