Bug #65994 [Com]: PDO::prepare() with multi-queries doesn't work inside a transaction

From: Date: Tue, 25 Feb 2014 18:11:24 +0000
Subject: Bug #65994 [Com]: PDO::prepare() with multi-queries doesn't work inside a transaction
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-184426@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=65994&edit=1

 ID:                 65994
 Comment by:         enrico_kaelert at kabelmail dot de
 Reported by:        pedro at sancao dot com dot br
 Summary:            PDO::prepare() with multi-queries doesn't work
                     inside a transaction
 Status:             Not a bug
 Type:               Bug
 Package:            PDO MySQL
 Operating System:   Linux/Windows
 PHP Version:        5.4.21
 Block user comment: N
 Private report:     N

 New Comment:

@uw@php.net
Whats the meaning of beginTransaction() and commit() then?
Adding those functions @PDO for single queries seems not "worth" to me.
Or do you recommend to execute each query like?:
function simpleExample(){
    $dbh->beginTransaction();
      
    $stmt = $dbh->prepare("INSERT INTO ...");  
    if(!$stmt->execute()){
        $dbh->rollBack();
        return false;
    }

    $stmt = $dbh->prepare("INSERT INTO ...");  
    if(!$stmt->execute()){
        $dbh->rollBack();
        return false;
    }

    $stmt = $dbh->prepare("INSERT INTO ...");  
    if(!$stmt->execute()){
        $dbh->rollBack();
        return false;
    }

    $dbh->commit();
    return true;        
}

And whats the security hole btw?


Previous Comments:
------------------------------------------------------------------------
[2014-02-25 12:56:53] uw@php.net

It makes no sense to even try multi query using a prepared statement API call. Multi query is not
available with PS. If at all it could be made working using query()/exec() but that would mean a
security hole and was for sure out of spec for PDO (given there was a proper PDO spec)

------------------------------------------------------------------------
[2014-02-01 01:04:24] enrico_kaelert at kabelmail dot de

Its weird but use 
$stmt = null;
and THEN fire the commit()

it works.
Created a detailed report here: https://bugs.php.net/bug.php?id=66621

btw: the bug page search function is crap =(

------------------------------------------------------------------------
[2013-10-29 14:12:22] pedro at sancao dot com dot br

Description:
------------
Using PDO on MySQL 5.5.32 (Linux and Windows)

When executing a prepared multi-query statement after calling PDO::beginTransaction() the
PDO::commit() will return true but no commit will be done.
Also the affected tables will be locked until the database server is restarted.

Nor error is raised neither exception is thrown.

My tests was within a try/catch block.

Test script:
---------------
$pdo = new PDO('mysql:dbname=PLACE_DATABASE;host=localhost;charset=utf8',
'PLACE_USER', 'PLACE_PASSWORD', array(
	PDO::ATTR_PERSISTENT => true,
	PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
	PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
));
try {
	$pdo->beginTransaction();
	$statement = $pdo->prepare('UPDATE ...; UPDATE ...; ');
	$statement->execute();
	$pdo->commit();
} catch (PDOException $e) {
	exit($e->getMessage());
	$pdo->rollBack();
}



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=65994&edit=1


Thread (4 messages)

« previous php.bugs (#184426) next »