MySQL Query with a while and another query inside

3

I raise here a question that I have consulted in other places and nobody has been able to answer me. I'm a newbie in PHP and MySQL, and I'm doing my first steps. I just found a fairly frequent case, which is a query that leads to a while, within which there is another query. If they have the SQL inside the PHP everything is fine; but as soon as I put the querys in stored procedures in the database, the second stops working.

A very simple example that can be contrasted:

// PRIMERA QUERY
$result_1 = $connection->query("SELECT * FROM user");
if($result_1){
    while ($row_1 = $result_1->fetch_object()){
        $id_1 = $row_1->id;
        echo "<b>id_1: ".$id_1."</b><br />";
        // SEGUNDA QUERY
        $result_2 = $connection->query("SELECT * FROM user");
        if($result_2){
            while ($row_2 = $result_2->fetch_object()){
                $id_2 = $row_2->id;
                echo "id_2: ".$id_2."<br />";
            }
            $result_2->close();
            $connection->next_result();
        }
    }
    $result_1->close();
    $connection->next_result();
}

This works perfectly, and the result is this:

id_1: 1
id_2: 1
id_2: 2
id_2: 3
id_2: 4
id_2: 5
id_1: 2
id_2: 1
id_2: 2
id_2: 3
id_2: 4
id_2: 5
id_1: 3
id_2: 1
id_2: 2
id_2: 3
id_2: 4
id_2: 5
etc.

Now I create a procedure in the database:

CREATE PROCEDURE test_procedure()
BEGIN
SELECT * from user;
END

And then let's say that I leave the first query with SQL and the second with a procedure call:

$result_1 = $connection->query("SELECT * FROM user");
[…]
$result_2 = $connection->query("CALL x_test()");
[…]

It also works perfectly, same result.

Peeeero if I put the first query with a call to the procedure and the second with the SQL inside:

$result_1 = $connection->query("CALL x_test()");
[…]
$result_2 = $connection->query("SELECT * FROM user");
[…]

Or if I put the two querys with calls to procedures:

$result_1 = $connection->query("CALL x_test()");
[…]
$result_2 = $connection->query("CALL x_test()");
[…]

Then the second query fails and the result is as follows:

id_1: 1
id_1: 2
id_1: 3
etc.

In summary: as long as the first query has the SQL inside, everything is fine; but if you put a call to a procedure, the second query will fail.

It seems so simple and basic to me that I do not understand how it can fail. Does anyone have any ideas?

    
asked by user47780 15.06.2017 в 15:11
source

1 answer

0

Assuming you're using the mysqli library:

$result_1 = $connection->query("CALL x_test()");
[…]
$result_2 = $connection->query("CALL x_test()") or die('Error en llamada: '. $connection->errno . ' - ' . $connection->error);

Probably the error message will be about releasing results before calling another stored procedure; I remember reading about this limitation, but I can not find the documentation now.

Short answer: You can not call a second stored procedure until you release the first one and, therefore, you can not nest them.

In any case, it is always a bad idea to nest queries in this way and we would have to look for the option to obtain all the data with a single query, whether using JOIN, UNION or another one that escapes me at the moment.

    
answered by 28.07.2017 в 21:46