Gruber
Gruber

Reputation: 4568

Calling MySQL stored routines more than once in PHP

I want to call multiple MySQL stored routines sequentially with PHP, but I can only call a single one successfully---after that I get the error Commands out of sync; you can't run this command now. Researching the problem I found it's a common headache (caused by not "finishing up" the previous call before the next one is made), but I didn't find a solution I could apply to my code:

for ($i=0; $i<10; $i+=1) 
{
    if (!($conn=getconn() && ($result=mysql_query("call MyStoredRoutine();", $conn))))
    {
        $errorMessage = mysql_error($conn); // Error 2nd time here...
    }
    else {
        while ($row=mysql_fetch_array($result)) {
            // Retrieve data here...
        }
    }
}

And the implementation of getconn() (I don't think it's relevant but you never know):

function getconn() {
    global $custdb, $custdb_connection, $dbuser, $dbpass;
    if (! $custdb_connection) {
        if (! ($custdb_connection = mysql_connect("localhost", $dbuser, $dbpass, true))) return false;
        if (! (@ mysql_select_db($custdb, $custdb_connection)))  return false;
    }
    return $custdb_connection;
}

PHP version is 5.3.8-1~dotdeb.1, MySQL is 5.1.54-1ubuntu4.

Anybody knows what I should do to my code to make it work?

Upvotes: 0

Views: 416

Answers (1)

Jacco
Jacco

Reputation: 23799

Maybe switch to MySQLi and use multi-query()

MySQLi gives you the added benefit of prepared statements and is build to support OOP.

Upvotes: 1

Related Questions