php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49058 Prepared statements with transaction support
Submitted: 2009-07-25 21:58 UTC Modified: 2009-09-03 01:00 UTC
From: aphtk at yahoo dot com Assigned: mysql (profile)
Status: No Feedback Package: MySQLi related
PHP Version: 5.2.10 OS: Ubuntu 9.04
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: aphtk at yahoo dot com
New email:
PHP Version: OS:

 

 [2009-07-25 21:58 UTC] aphtk at yahoo dot com
Description:
------------
Prepared statements fail but transaction commits other sql inserts and do not return a failure code.

I am trying to insert to a (innodb) table with unique constraint. 

The 1st time insert proceeds as planned.
On page refresh...
1st sql fails but no failure code is returned
2nd sql proceeds and inserts successfully
3rd sql fails but still no code is rolled back


Reproduce code:
---------------
$u = new userDAO();
//$u->createNewUser($first_name, $last_name, $username, $encrypted_passwd, $reminder_question, $reminder_hint);
$u->createNewUser("Anang1", "Phatak1", "anang.phatak@gmail.com", 1, "quake2", "Is this a test ?", "test");




userDAO.php

<?php
require_once("Connection.php");


class userDAO {
	private $_connection;
	
	
	public function __construct() {
		$this->_connection = Connection::getConnection("localhost", "terminator", "terminate");	
	}
	
	
	public function readUser($username) {
		$sql = "SELECT * FROM xt_user_details_vw WHERE username = ?";
		
		if ($stmt = $this->_connection->prepare($sql)) {
			$stmt->bind_param("s",$username);
			$stmt->execute();
			$stmt->store_result();
			
			$meta = $stmt->result_metadata();
			
			$fields = array();
			while ($field = $meta->fetch_field()) { 
				$fields[] = &$row[$field->name];
	        }
			
			call_user_func_array(array($stmt, 'bind_result'), $fields);
			
			$datagrid = array();
			$row_num = 0; 
			while ($stmt->fetch()) {
				$datagrid[$row_num] = array();
				foreach ($row as $k=>$v) {
					$datagrid[$row_num][$k] = $v;
				}
			}
			$stmt->free_result();
			$stmt->close();
			 
			return $datagrid;
		} 
		else {
			throw new Exception("Error occured while attempting to run user read query (userDAO.php)","56");
		}
		
	}
	
	protected function addNewUserInfo($first_name, $last_name, $username, $active_flag=1){
		$user_insrt_sql = "INSERT INTO xtusers (first_name, last_name, username, active_flag) VALUES (?, ?, ?, ?)";
		
		try {
			if ($stmt = $this->_connection->prepare($user_insrt_sql)) {
				$stmt->bind_param("sssi", $first_name, $last_name, $username, $active_flag);
				$stmt->execute();
				$stmt->close();
				return $this->_connection->insert_id;
			}
			else {
				throw new Exception("SQL Error");	
			}
		}
		catch (Exception $err) {
			$this->_connection->rollback();
			echo ($err->getMessage());
			throw $err;
		}
		
	}
	
	protected function addPasswordInfo($encrypted_passwd) {
		$password_insrt_sql	= "INSERT INTO xtpasswords (passwd) VALUES (?)";
		try {
			if ($stmt = $this->_connection->prepare($password_insrt_sql)) {
				$stmt->bind_param("s", $encrypted_passwd);
				$stmt->execute();
				$stmt->close();
				echo ($this->_connection->sqlstate."<br/>");
				return $this->_connection->insert_id;
			}
			else {
				throw new Exception("SQL Error");
			}
		}
		catch (Exception $err) {
			$this->_connection->rollback();
			echo ($err->getMessage());
			throw $err;
		}
	}
	
	protected function addPasswordMetadata($user_id, $passwd_id, $reminder_question, $reminder_hint) {
		$usrpass_insrt_sql		= "INSERT INTO xtuser_passwords ";
		$usrpass_insrt_sql	   .= "(user_id, passwd_id, reminder_question, reminder_hint)"; 
		$usrpass_insrt_sql	   .= "values (?, ?, ?, ?)";
		
		
		try {
			if ($stmt = $this->_connection->prepare($usrpass_insrt_sql)) {
				echo ($usrpass_insrt_sql."<br />");
				echo ("'".$user_id."','".$passwd_id."','".$reminder_question."','".$reminder_hint."'");
				$stmt->bind_param("iiss", $user_id, $passwd_id, $reminder_question, $reminder_hint);
				$stmt->execute();
				$stmt->close();
				return $this->_connection->insert_id;
			}
			else {
				throw new Exception("SQL Error");
			}
			
		}
		catch(Exception $err) {
			$this->_connection->rollback();
			echo ($err->getMessage());
			throw $err;
		}
	}
	
	
	public function createNewUser($first_name, $last_name, $username, $active_flag, $encrypted_passwd, $reminder_question, $reminder_hint){
		$this->_connection->autocommit(FALSE);
		try {
			$user_insrt_sql = "INSERT INTO xtusers (first_name, last_name, username, active_flag) VALUES (?, ?, ?, ?)";
				$stmt = $this->_connection->prepare($user_insrt_sql);
				$stmt->bind_param("sssi", $first_name, $last_name, $username, $active_flag);
				$stmt->execute();
				$stmt->close();
				
			$tmp_user_id = $this->_connection->insert_id;
			echo ("Added user id :". $tmp_user_id ."<br />");
			
			$password_insrt_sql	= "INSERT INTO xtpasswords (passwd) VALUES (?)";
				$stmt = $this->_connection->prepare($password_insrt_sql);
				$stmt->bind_param("s", $encrypted_passwd);
				$stmt->execute();
				
			$tmp_passwd_id = $this->_connection->insert_id;	
			echo ("Added password id :". $tmp_passwd_id ."<br />");
			
			$usrpass_insrt_sql		= "INSERT INTO xtuser_passwords ";
			$usrpass_insrt_sql	   .= "(user_id, passwd_id, reminder_question, reminder_hint)"; 
			$usrpass_insrt_sql	   .= "VALUES (?, ?, ?, ?)";
				
				$stmt = $this->_connection->prepare($usrpass_insrt_sql);
				$stmt->bind_param("iiss", $tmp_user_id, $tmp_passwd_id, $reminder_question, $reminder_hint);
				$stmt->execute();
				$stmt->close();
			$tmp_passwdmetadata_id = $this->_connection->insert_id;
			echo ("Added password metadata id :". $tmp_passwdmetadata_id ."<br />");
			$this->_connection->commit();
		}
		catch(exception $err) {
			$this->_connection->rollback();
			echo ("<hr />". "an exception occurred".$err->getMessage());
		}
	}
	
	
	public function updateUser($user_id, $first_name, $last_name, $username, $encrypted_passwd, $reminder_question, $reminder_hint) {
					
	}
	
	public function deleteUser() {
	
	}
}
?>

Expected result:
----------------
sql should be rolled back

Actual result:
--------------
1st sql fails but no failure code is returned
2nd sql proceeds and inserts successfully
3rd sql fails but still no code is rolled back

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-08-26 08:58 UTC] uw@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.


 [2009-09-03 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Tue Jan 31 18:03:43 2023 UTC