# [PATCH] mysqli #35203 / #48065 Eliminate special case for calling procedures in mysqli

- Thread: https://externals.io/message/43773
- Original archive: https://news-web.php.net/php.internals/43773
- Started by Michael G Schwern on 2009-04-26T04:51:01+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #43773 — Michael G Schwern — 2009-04-26T04:51:01+00:00

URL: https://externals.io/message/43773#43773

This is a patch against 5.2.9 to fix mysqli::query so a user can call stored
procedures the same as they do any other statement.  No more multi_query() and
next_result() work arounds necessary to avoid a "Commands out of sync" error.

I note this has been rejected several times in the tracker as not being a bug.
 I feel its a clear encapsulation violation making the user special case a
query just because it's calling a stored procedure.  Aside from just being
very inconvenient, some API wrappers around mysqli, Drupal 6 for example,
leave the user with no way to get at mysqli::multi_query().

Anyhow, here's a complete patch with a test.  The technique and code is lifted
from Perl's DBD::mysql driver, which you can see here as
mysql_st_free_result_sets()
http://cpansearch.perl.org/src/CAPTTOFU/DBD-mysql-4.011/dbdimp.c

Before each query it frees any results still hanging around on the connection,
which is essentially what a user has to do.  I'm not really a C programmer so
I left most of the code as is, do/while loop and all.

I'm not sure how mysqli normally handles errors so I just went with a `printf()`
and return trusting that you'll fix that up.  It causes test 057 to fail
because that test deliberately generates an out of sync error, which my code
diligently prints.  So that should go away with fixed error handling.

Thanks,
Schwern


-- 
THIS I COMMAND!

---

## #43774 — Andrey Hristov — 2009-04-26T07:17:30+00:00

URL: https://externals.io/message/43773#43774 · In reply to: #43773

Hi,
could you show how you want to express that in PHP code that will work 
with calling prepared statements. How the API should work? (Example PHP 
code that uses the new API).


Best,
Andrey

Michael G Schwern wrote:
> This is a patch against 5.2.9 to fix mysqli::query so a user can call stored
> procedures the same as they do any other statement.  No more multi_query() and
> next_result() work arounds necessary to avoid a "Commands out of sync" error.
> 
> I note this has been rejected several times in the tracker as not being a bug.
>  I feel its a clear encapsulation violation making the user special case a
> query just because it's calling a stored procedure.  Aside from just being
> very inconvenient, some API wrappers around mysqli, Drupal 6 for example,
> leave the user with no way to get at mysqli::multi_query().
> 
> Anyhow, here's a complete patch with a test.  The technique and code is lifted
> from Perl's DBD::mysql driver, which you can see here as
> mysql_st_free_result_sets()
> http://cpansearch.perl.org/src/CAPTTOFU/DBD-mysql-4.011/dbdimp.c
> 
> Before each query it frees any results still hanging around on the connection,
> which is essentially what a user has to do.  I'm not really a C programmer so
> I left most of the code as is, do/while loop and all.
> 
> I'm not sure how mysqli normally handles errors so I just went with a `printf()`
> and return trusting that you'll fix that up.  It causes test 057 to fail
> because that test deliberately generates an out of sync error, which my code
> diligently prints.  So that should go away with fixed error handling.
> 
> Thanks,
> Schwern

---

## #43776 — Michael G Schwern — 2009-04-26T07:47:02+00:00

URL: https://externals.io/message/43773#43776 · In reply to: #43774

Andrey Hristov wrote:
> could you show how you want to express that in PHP code that will work
> with calling prepared statements. How the API should work? (Example PHP
> code that uses the new API).

Sorry, I don't understand what you're asking.

There is no changes to the mysqli API in this patch, its just a bug fix for
mysqli::query().  If you're asking for a demonstration of the bug, there's a
phpt test in the patch and demo code in the bugs.
http://bugs.php.net/bug.php?id=35203
http://bugs.php.net/bug.php?id=48065

Here's a stand alone demo of the problem this patch is fixing.

<?php

// Just a simple SQL statement for testing.
function stmt() {
    return "select 1";
}

// Create a stored procedure around our testing statement.
function setup_database() {
    $mysqli = connection();

    $sql = stmt();
    $mysqli->query("DROP PROCEDURE IF EXISTS test_bug");
    $mysqli->query("
CREATE PROCEDURE test_bug()
BEGIN
    $sql;
END
");
    print $mysqli->error;
}

// Get a database connection.
// Assumes a running MySQL 5 server on localhost with
// a test account and database (no password) which can
// drop and create procedures.
function connection() {
    return new mysqli("localhost", "test", "", "test");
}

// Using query() to call two stored procedures using query()
// will fail with "Commands out of sync"
function test_double_query($query) {
    $mysqli = connection();

    if( !$mysqli->query($query) )
        printf("First query FAILED: %s\n", $mysqli->error);

    if( !$mysqli->query($query) )
        printf("Second query FAILED: %s\n", $mysqli->error);

    printf("ok - $query\n\n");
}


setup_database();

// querying using statements or a procedure which contains
// the same statement should work the same.  It doesn't.
test_double_query("call test_bug");
test_double_query(stmt());

?>



-- 
The interface should be as clean as newly fallen snow and its behavior
as explicit as Japanese eel porn.

---

## #43964 — Michael G Schwern — 2009-05-14T23:04:13+00:00

URL: https://externals.io/message/43773#43964 · In reply to: #43773

Ping?

There's a fully formed patch here, with tests, to fix a mysqli bug.  I haven't
gotten any feedback.

Here's the original message with the patch.
http://news.php.net/php.internals/43773


Michael G Schwern wrote:
> This is a patch against 5.2.9 to fix mysqli::query so a user can call stored
> procedures the same as they do any other statement.  No more multi_query() and
> next_result() work arounds necessary to avoid a "Commands out of sync" error.
> 
> I note this has been rejected several times in the tracker as not being a bug.
>  I feel its a clear encapsulation violation making the user special case a
> query just because it's calling a stored procedure.  Aside from just being
> very inconvenient, some API wrappers around mysqli, Drupal 6 for example,
> leave the user with no way to get at mysqli::multi_query().
> 
> Anyhow, here's a complete patch with a test.  The technique and code is lifted
> from Perl's DBD::mysql driver, which you can see here as
> mysql_st_free_result_sets()
> http://cpansearch.perl.org/src/CAPTTOFU/DBD-mysql-4.011/dbdimp.c
> 
> Before each query it frees any results still hanging around on the connection,
> which is essentially what a user has to do.  I'm not really a C programmer so
> I left most of the code as is, do/while loop and all.
> 
> I'm not sure how mysqli normally handles errors so I just went with a `printf()`
> and return trusting that you'll fix that up.  It causes test 057 to fail
> because that test deliberately generates an out of sync error, which my code
> diligently prints.  So that should go away with fixed error handling.
> 
> Thanks,
> Schwern
> 
> 


-- 
ROCKS FALL! EVERYONE DIES!
	http://www.somethingpositive.net/sp05032002.shtml
