PHP Classes

File: documentation/transaction-example.php

Recommend this page to a friend!
  Classes of Camilo Sperberg  >  Extended MySQLi  >  documentation/transaction-example.php  >  Download  
File: documentation/transaction-example.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Extended MySQLi
MySQL database access wrapper using MySQLi
Author: By
Last change: Some fixes related all with composer
Version 4.1.0 of the class, several fixes
Date: 8 years ago
Size: 2,172 bytes


Class file image Download

 * This example file has been temporarily disabled because it relies on the fact that we have some more classes loaded,
 * which must be transformed to the new way of implementing dependencies



$u4uLoader = new \u4u\autoLoader();
$bench = new \u4u\benchmark('databaseCalls');
$db = new \u4u\db_mysqli();

echo '<pre>';

try {
    $db->query('DROP TABLE IF EXISTS t');
    $db->query('CREATE TABLE t(i int(11) PRIMARY KEY) engine=InnoDB');
    $db->query('INSERT INTO t VALUES (?)',1);
    $db->query('INSERT INTO t VALUES (?)',2);
} catch (\u4u\queryException $e) {
    printf($e->getMessage().'<br />');
printf('<em>First transaction ended, starting with second (which will be the one to fail)</em><br />');

try {
    $db->query('INSERT INTO t VALUES (?)',3);
    $db->query('INSERT INTO t VALUES (?)',4);
    $db->query('INSERT INTO t VALUES (?)',2);
    $db->query('INSERT INTO t VALUES (?)',5);
} catch (\u4u\queryException $e) {
    printf('Transaction failed! The message delivered by the database is: '.$e->getMessage().'<br />');
printf('<em>Second transaction ended, error check:</em><br />');


print('<br /><em>Database version:</em><br />');
try {
    echo $db->version();
    print('<br />');
} catch (\Exception $e) {
    print_r($e->getMessage().'<br />');

printf('End of execution<br />');
printf('Total time: %f<br />', $bench->getDiff('databaseCalls', 'time'));
printf('New table creation: %f<br />', $bench->getDiff('newTable', 'time'));
printf('Exception time: %f<br />', $bench->getDiff('databaseException', 'time'));
printf('Version time: %f<br />', $bench->getDiff('databaseVersion', 'time'));
echo '</pre>';