php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50483 mysqli_error() does not return error while using with mysqli_real_query()
Submitted: 2009-12-15 16:28 UTC Modified: 2009-12-21 17:06 UTC
From: pcdinh at gmail dot com Assigned:
Status: Not a bug Package: MySQLi related
PHP Version: 5.3.1 OS: Windows XP
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: pcdinh at gmail dot com
New email:
PHP Version: OS:

 

 [2009-12-15 16:28 UTC] pcdinh at gmail dot com
Description:
------------
mysqi_error() and mysqli_errno() does not capture last error message 
and error code if mysqli_real_query() executes on a stored procedure 
that returns multiple result sets but one of them is an error. 

As you can see from the below code, I have a SP that basically a set 
of SELECTs but the last one is invalid by intention. If executing the 
SP (e.x: CALL p2(2, 5) ) in a MySQL Query Manager, the following 
warning will show up:

(1 row(s) returned)
Execution Time : 00:00:00:000
Transfer Time  : 00:00:00:000
Total Time     : 00:00:00:000

(1 row(s) returned)
Execution Time : 00:00:00:000
Transfer Time  : 00:00:00:000
Total Time     : 00:00:00:000

(1 row(s) returned)
Execution Time : 00:00:00:000
Transfer Time  : 00:00:00:000
Total Time     : 00:00:00:000

Query : CALL p2(2, 5) 
Error Code : 1146
Table 'test.non_existent_' doesn't exist
Execution Time : 00:00:00:000
Transfer Time  : 00:00:00:000
Total Time     : 00:00:00:000

However, mysqli does not work that way. It does not know what happens 
with the last query. It simply knows the last query does not return a 
result set.



Reproduce code:
---------------
<?php

/*

DELIMITER $$

CREATE PROCEDURE p2(X INT, Y INT)
    	DETERMINISTIC
    	BEGIN
        	SELECT
            	X ;
        	SELECT
            	X AS first_param,
            	Y AS second_param;
        	SELECT
            	X,
            	Y,
           		X + Y AS sum_xy,
            	X * Y AS prod_xy;
        	SELECT 1 FROM non_existent_; -- Intentional error
    	END$$

DELIMITER ;
*/


$conn = mysqli_connect("localhost", "root", "123456", "test");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query   = "CALL p2(2, 5)";
$success = mysqli_real_query($conn, $query);

if (false === $success)
{
    var_dump(mysqli_error($conn));
}

$i = 0;
do
{
    $result = mysqli_store_result($conn);
    if (false !== $result)
    {
        echo ++$i."\n";
    }
    else
    {
        var_dump(mysqli_error($conn));
    }

} while (mysqli_more_results($conn) && mysqli_next_result($conn));

/* close connection */
mysqli_close($conn);

?>

Expected result:
----------------
1
2
3
string(0) "Table 'test.non_existent_' doesn't exist"

Actual result:
--------------
1
2
3

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-12-21 16:06 UTC] pcdinh at gmail dot com
Could someone look at this issue please?
 [2009-12-21 17:06 UTC] andrey@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Hi, 
I checked your script and found out that there is a bug in it.
You need to check for an error after the loop. If you do a var_dump(mysqli_error($conn)); you will get the error, like here:
string(40) "Table 'test.non_existent_' doesn't exist"

As after the third query there is no result set the loop ends and you can check whether there was an error.

Thanks!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 10:01:28 2024 UTC