#46144 [Com]: failed prepare() does not fill error and errno fields

From: Date: Mon, 18 May 2009 14:43:14 +0000
Subject: #46144 [Com]: failed prepare() does not fill error and errno fields
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-137442@lists.php.net to get a copy of this message
ID: 46144 Comment by: Progman2002 at gmx dot de Reported By: Progman2002 at gmx dot de Status: Open Bug Type: MySQLi related Operating System: Linux PHP Version: 5.2CVS-2009-01-25 (snap) New Comment: Sure I can call close() by myself (which I normally do), but it doesn't solve the bug itself. The point is you get an error situation and don't know why. Previous Comments: ------------------------------------------------------------------------ [2009-05-18 02:17:56] felix9x at yahoo dot com It's because the first $stmt object is destroyed by the second assignment (which clears the last error message). $sql = 'INSERT INTO SomeTest(UserID, RechtID) WHERE (?,?)'; if (!$stmt = $db->prepare($sql)) { die($db->error."-".$db->errno."-".$db->info); } Its equivalent to doing this: $stmt = false; The destructor of the Mysqli_stmt class resets the Last error. Its possible to call $stmt->close() explicitly. Probably best to use this syntax: $sql = 'INSERT INTO t(i) WHERE (?)'; $stmt = $db->stmt_init(); if(!$stmt->prepare($sql) ){ die( $stmt->error ); } ------------------------------------------------------------------------ [2009-01-25 10:50:41] Progman2002 at gmx dot de The bug is still not fixed. Maybe it has something to do with an uncalled destructor since I use the same variable $stmt. Actual result: -------------- PHP-Version: 5.2.9-dev MySQL-Server-Version: 50060 MySQL-Protocol: 10 -0- Expected result: ---------------- PHP-Version: 5.2.9-dev MySQL-Server-Version: 50060 MySQL-Protocol: 10 (Showing a MySQL error which says "Syntax error near WHERE (?,?)" or says sth. like "unfinished prepare statement before") ------------------------------------------------------------------------ [2008-11-29 10:59:24] Progman2002 at gmx dot de As the paste on the pasteboard is gone I'll add the code here. ----------- <?php error_reporting(E_ALL); ini_set('display_errors', 1); $db = @new MySQLi('localhost', '', '', 'test'); if (mysqli_connect_errno()) { die('Konnte keine Verbindung zu Datenbank aufbauen, MySQL meldete: '.mysqli_connect_error()); } echo 'PHP-Version: '.PHP_VERSION."\n"; echo 'MySQL-Server-Version: '.$db->server_version."\n"; echo 'MySQL-Protocol: '.$db->protocol_version."\n"; $sql = 'CREATE TEMPORARY TABLE SomeTest(UserID INT NOT NULL, RechtID INT NOT NULL)'; if (!$db->query($sql)) { die($db->error); } $sql = 'DELETE FROM SomeTest WHERE UserID = ?'; if (!$stmt = $db->prepare($sql)) { die($db->error); } // note the missing $stmt->close() here $sql = 'INSERT INTO SomeTest(UserID, RechtID) WHERE (?,?)'; if (!$stmt = $db->prepare($sql)) { die($db->error."-".$db->errno."-".$db->info); } echo "done"; ?> ------------------------------------------------------------------------ [2008-09-21 13:49:28] Progman2002 at gmx dot de Description: ------------ If you create a prepared statement with a DELETE query and tries to create a second prepared statement with an INSERT query on the same table without closing the first one the MySQLi::prepare() method failed, but the fields $error and $errno (and all other related to them) aren't filled with the error message. This is strange as the prepare() failed but you dont know why. The mysql error is shown if I save the second statement into another variable (like if (!$stmt2 = $db->prepare($sql))) (maybe its related to bug #44766) Reproduce code: --------------- Code is at http://nopaste.php-quake.net/51976 Expected result: ---------------- PHP-Version: 5.2.6-pl7-gentoo MySQL-Server-Version: 50042 MySQL-Protocol: 10 {Showing a MySQL error which says "Syntax error near WHERE (?,?)" or says sth. like "unfinished prepare statement before") Actual result: -------------- PHP-Version: 5.2.6-pl7-gentoo MySQL-Server-Version: 50042 MySQL-Protocol: 10 -0- (so the values are all empty strings or zero) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=46144&edit=1

« previous php.bugs (#137442) next »