Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- /* contact: cottton (@i-stats.net or ts3:i-stats.net)
- EXPLANATION:
- using PDO to fire more than 1 "INSERT INTO" via PDO::beginTransaction() and PDO::commit()
- Q: what does it mean?
- A: this means i try to send more than 1 query to the mysql server at once:
- $dbh->beginTransaction();
- $sql = "
- INSERT INTO `testdb`.`tbl` SET `name` = 'a';
- INSERT INTO `testdb`.`tbl` SET `name` = 'b';
- INSERT INTO `testdb`.`tbl` SET `name` = 'c';
- ";
- $stmt = $dbh->prepare($sql);
- $stmt->execute();
- $dbh->commit();
- Q: does this work?
- A: no. somehow the mysql server still waits for the commit.
- THE TEST SCRIPT:
- Q: is it 10 to 20 lines?
- A: no. hell! i cant write you that test script in 20 lines.
- explanation:
- there are 2 ways we will fire this multi-query:
- 1. regular as above
- 2. a alternative way: we "destroy" the $stmt before we fire the ->commit()
- Q: how do i use this test script?
- A: just adapt the settings:
- $mysql_username, $mysql_password, $mysql_host and the $type
- Q: for what is the $type for?
- A: it allows you to switch between the 2 ways we use to fire the multi-query.
- so you will first look at the regular way to see that the multi-inserts didnt work,
- and then switch to the alternative way to see that they surprisingly worked.
- (!) please be sure you truncated the table after using the alternative way
- i wont do that for you ...
- Q: how do i see the result?
- A: in your browser =)
- you dont really have to read the script.
- the output shows you what happen.
- so here is the script:
- (and this is the last time ill try to explain you that bug, sorry but ...)
- */
- # db settings:
- $mysql_username = 'username';
- $mysql_password = 'password';
- $mysql_host = 'localhost';
- # switch here the $type (un-/comment one of those):
- $type = 'regular'; # regular way
- #$type = 'alternative'; # alternative way
- echo '<h1>using PDO to insert multi-querys using beginTransaction() and commit()</h1>';
- # 1. connect
- try{
- $dbh = new PDO('mysql:host='.$mysql_host,$mysql_username,$mysql_password);
- }catch(PDOException $e){
- stop($e->getMessage());
- }
- echo 'connected to mysql server<br>';
- # 2. create database and table for tests
- foreach(
- array(
- "CREATE SCHEMA IF NOT EXISTS `testdb`;",
- "CREATE TABLE IF NOT EXISTS `testdb`.`tbl`(
- `id` INT unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
- `name` VARCHAR(16) NOT NULL
- );"
- ) as $query
- ){
- $stmt = $dbh->prepare($query);
- if(!$stmt->execute()){
- stop($stmt->errorInfo());
- }
- }
- echo 'created database and table if not already exists<br>';
- # 3. multi insert via beginTransaction() and commit() / rollBack()
- $dbh->beginTransaction();
- echo '<b>$dbh->beginTransaction();</b><br>';
- $sql = "
- INSERT INTO `testdb`.`tbl` SET `name` = 'a';
- INSERT INTO `testdb`.`tbl` SET `name` = 'b';
- INSERT INTO `testdb`.`tbl` SET `name` = 'c';
- ";
- echo '<b>INSERT INTO `testdb`.`tbl` SET `name` = \'a\';</b><br><b>INSERT INTO `testdb`.`tbl` SET `name` = \'b\';</b><br><b>INSERT INTO `testdb`.`tbl` SET `name` = \'c\';</b><br>';
- $stmt = $dbh->prepare($sql);
- echo '<b>$stmt = $dbh->prepare($sql);</b><br>';
- if(!$stmt->execute()){
- $dbh->rollBack();
- stop($stmt->errorInfo());
- }else{
- ############################
- # INTERESTING PART IS HERE #
- ############################
- echo '<br><i>we will now ->commit()</i><br>';
- switch($type){
- case 'regular':
- echo '<h2>using regular way to see that the multi-inserts didnt work</h2>';
- $dbh->commit();
- echo 'fired <b>$dbh->commit();</b><br>';
- break;
- case 'alternative':
- echo '<h2>using the alternative way to see that they surprisingly worked</h2>';
- $stmt = null;
- echo '"destroyed" the $stmt: <b>$stmt = null;</b><br>';
- $dbh->commit();
- echo 'and now fired <b>$dbh->commit();</b><br>';
- break;
- default:
- stop('wrong type! use "regular" or "alternative"');
- break;
- }
- }
- # 4. now (have to) disconnect and reconnect to see if the INSERTS really worked:
- # (you may try your self without reconnecting...)
- $dbh = NULL;
- try{
- $dbh = new PDO('mysql:host='.$mysql_host,$mysql_username,$mysql_password);
- }catch(PDOException $e){
- stop($e->getMessage());
- }
- echo 'reconnected to mysql server<br>';
- ## 5. read tbl
- echo 'read tbl: <b>SELECT * FROM `testdb`.`tbl`;</b><br>';
- $stmt = $dbh->prepare("SELECT * FROM `testdb`.`tbl`;");
- if(!$stmt->execute()){
- stop($stmt->errorInfo());
- }else{
- while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
- $res[] = $row;
- }
- echo '<b>result:</b><br>';
- if(!isset($res)){
- echo 'we got no data back from the mysql server<br>';
- }else{
- echo '<pre>';
- print_r($res);
- echo '</pre>';
- }
- }
- # e-handling
- function stop($e){
- echo '<pre>';
- print_r($e);
- echo '</pre>';
- exit(0);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment