Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- # PDO::commit() bug(?) -- cottton @i-stats.net
- # db settings:
- $user = 'username';
- $pw = 'password';
- $host = 'localhost';
- # switch here between "bug(?) version" and "working version"
- #
- $type = 'bug'; # bug(?) version
- #$type = 'working'; # how it works
- # 1. connect
- #
- try{
- $dbh = new PDO('mysql:host='.$host,$user,$pw);
- }catch(PDOException $e){
- stop($e->getMessage());
- }
- # 2. create database and table for tests
- #
- foreach(
- array(
- "CREATE SCHEMA IF NOT EXISTS `databasename`;",
- "CREATE TABLE IF NOT EXISTS `databasename`.`testtable`(
- `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());
- }
- }
- # 3. multi insert via beginTransaction() and commit() / rollBack()
- #
- $dbh->beginTransaction();
- $sql = "
- INSERT INTO `databasename`.`testtable` SET name = :name1;
- INSERT INTO `databasename`.`testtable` SET name = :name2;
- INSERT INTO `databasename`.`testtable` SET name = :name3;
- ";
- $para = array(
- 'name1' => 'hans',
- 'name2' => 'klaus',
- 'name3' => 'olaf'
- );
- $stmt = $dbh->prepare($sql);
- if(!$stmt->execute($para)){
- $dbh->rollBack();
- stop($stmt->errorInfo());
- }else{
- switch($type){
- case 'bug':
- echo '<h1>bug(?) "version" (INSERTS wont be done):</h1>';
- test($stmt,'$stmt before $dbh->commit()');
- test($dbh->commit(),'fired $dbh->commit()');
- test($stmt,'$stmt after $dbh->commit()');
- break;
- case 'working':
- echo '<h1>working "version" (INSERTS will be done):</h1>';
- test($stmt,'$stmt before $dbh->commit()');
- test($stmt = 'string','changed $stmt to: $stmt = \'string\'');
- test($dbh->commit(),'fired $dbh->commit()');
- test($stmt,'$stmt after $dbh->commit()');
- break;
- default:
- stop('wrong type! use "bug" or "working"');
- break;
- }
- }
- # 4. now disconnect and reconnect to see if the INSERTS really worked:
- #
- $dbh = NULL;
- try{
- $dbh = new PDO('mysql:host=localhost',$user,$pw);
- }catch(PDOException $e){
- stop($e->getMessage());
- }
- ## 5. read tbl
- #
- $stmt = $dbh->prepare("SELECT * FROM `databasename`.`testtable`;");
- if(!$stmt->execute()){
- stop($stmt->errorInfo());
- }else{
- $result = null;
- while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
- $result[] = $row;
- }
- test($result,'SELECT * FROM `databasename`.`testtable`;');
- }
- # thats it
- function stop($e){
- test($e);;
- exit(0);
- }
- function test($val,$title=''){ # just a dubug func ... life easier ...
- $br = '<br />';
- echo '<pre># <b>DEBUG</b>: <u>'.$title.'</u>'.$br;
- echo '<i>type:</i> '.gettype($val).$br.'<i>val:</i> ';
- if(is_bool($val)){
- echo ($val === true)?'true':'false';
- }elseif(is_null($val)){
- echo 'NULL';
- }elseif(!is_numeric($val)and empty($val)){
- echo 'empty';
- }elseif(is_resource($val)){
- echo get_resource_type($val);
- }else{
- print_r($val);
- }
- echo $br.'#/DEBUG</pre>';
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement