<p>The source of this example is...</p>
 
<pre><code>
 
require_once "db.php";
 
$db = new db();
 
 
// We do NOT want to cache results when using transactions
 
$db->set_opts(array('cache_results' => false));
 
 
// Building the query
 
$sql = array(
 
    'test1' => 'val_erw',
 
    'test3' => 'val_24'
 
);
 
 
$where = array(
 
    'test2' => '242'
 
);
 
 
// Generating the UPDATE statement
 
$update = $db->build_update('test', $sql, $where);
 
 
// Printing the query used
 
echo $update;
 
 
// Starting the transaction
 
$db->transaction('BEGIN');
 
 
// Carrying out the UPDATE statement
 
$result = $db->resource($update);
 
 
if($result === true)
 
{
 
    $db->transaction('COMMIT');
 
    echo "<p>Table updated successfully!</p>";
 
}
 
else
 
{
 
    $db->transaction('ROLLBACK');
 
    echo "<p>There was an error updating the database!</p>";
 
}
 
 
$result = $db->fetch_rowset('SELECT * FROM test ORDER BY test2 ASC');
 
 
print_r($result);</code>
 
<p style="font-size: 20px;">And the result:</p>
 
<?php
 
require_once "db.php";
 
$db = new db();
 
 
// We do NOT want to cache results when using transactions
 
$db->set_opts(array('cache_results' => false));
 
 
// Building the query
 
$sql = array(
 
    'test1' => 'val_erw',
 
    'test3' => 'val_24'
 
);
 
 
$where = array(
 
    'test2' => '242'
 
);
 
 
// Generating the UPDATE statement
 
$update = $db->build_update('test', $sql, $where);
 
 
// Printing the query used
 
echo $update;
 
 
// Starting the transaction
 
$db->transaction('BEGIN');
 
 
// Carrying out the UPDATE statement
 
$result = $db->resource($update);
 
 
if($result === true)
 
{
 
    $db->transaction('COMMIT');
 
    echo "<p>Table updated successfully!</p>";
 
}
 
else
 
{
 
    $db->transaction('ROLLBACK');
 
    echo "<p>There was an error updating the database!</p>";
 
}
 
 
$result = $db->fetch_rowset('SELECT * FROM test ORDER BY test2 ASC');
 
 
print_r($result);
 
?></pre>
 
 |