<?php
use model\cache\Timeout;
use model\db\DBResult;
use model\db\DB;
$DB = DB::getInstance();
# insert method
#########################################################
$sql = 'INSERT INTO test (a,b,c) VALUES (?,?,?)';
$paramArr = array(
(int) "22",
(string) "aa",
(double) 44.33
);
$DB->startTransaction();
try{
$DB->insert($sql, $paramArr);
$DB->commit();
echo "auto Increment or sequence ID : ".$DB->last_id();
}catch (PDOException $error){
$DB->rollback();
trigger_error($error->getMessage(),E_USER_ERROR);
}
#########################################################
# update method
#########################################################
$sql = 'UPDATE test SET a=? WHERE id=?';
$paramArr = array('xxxx',22);
$DB->startTransaction();
try{
$DB->update($sql, $paramArr);
$DB->commit();
}catch (PDOException $error){
$DB->rollback();
trigger_error($error->getMessage(),E_USER_ERROR);
}
#########################################################
# delete method
#########################################################
$sql = 'DELETE FROM test WHERE id=?';
$paramArr = array(22);
$DB->startTransaction();
try{
$DB->delete($sql, $paramArr);
$DB->commit();
}catch (PDOException $error){
$DB->rollback();
trigger_error($error->getMessage(),E_USER_ERROR);
}
#########################################################
# select method
#########################################################
$sql = 'SELECT a,b,c FROM test WHERE id > ?';
$result = new DBResult();
$DB->select($result,$sql, array(1));
echo '<table>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
';
for($i=0;$i<$result->num_rows;$i++){
echo '<tr>
<td>'.$result->dataArr[$i]['a'].'</td>
<td>'.$result->dataArr[$i]['b'].'</td>
<td>'.$result->dataArr[$i]['c'].'</td>
</tr>';
}
echo '<table>';
# select + memcached method
$sql = 'SELECT a,b,c FROM test WHERE id > ?';
$result = new DBResult();
$DB->select($result,$sql, array(1),Timeout::get(1, Timeout::MINUTE));
echo '<table>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
';
for($i=0;$i<$result->num_rows;$i++){
echo '<tr>
<td>'.$result->dataArr[$i]['a'].'</td>
<td>'.$result->dataArr[$i]['b'].'</td>
<td>'.$result->dataArr[$i]['c'].'</td>
</tr>';
}
echo '<table>';
|