SOR_FWDONLY)) {
if ($sql) {
$sql .= ' limit ' . (int) $limit[0] . ',' . (intval($limit[1]) > 0 ? intval($limit[1]) : 10);
$pdoStatement = $this->pdo->prepare($sql, $preType);
$pdoStatement->execute($searchData);
return $data = $pdoStatement->fetchAll($dataMode);
} else {
return false;
}
}
public function insert($tableName, $data, $returnInsertId = false, $replace = false) {
if(!empty($tableName) && count($data) > 0){
$sql = $replace ? 'REPLACE INTO ' : 'INSERT INTO ';
list($setSql, $mapData) = $this->FDFields($data);
$sql .= $tableName . ' set ' . $setSql;
$pdoStatement = $this->pdo->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$execRet = $pdoStatement->execute($mapData);
return $execRet ? ($returnInsertId ? $this->pdo->lastInsertId() : $execRet) : false;
} else {
return false;
}
}
public function update($tableName, $data, $condition, $mapData = array(), $returnRowCount = true) {
if(!empty($tableName) && count($data) > 0) {
$sql = 'UPDATE ' . $tableName . ' SET ';
list($setSql, $mapSetData) = $this->FDFields($data);
$sql .= $setSql;
$mapData = array_merge($mapData, $mapSetData);
list($where, $mapData) = $this->FDCondition($condition, $mapData);
$sql .= $where ? ' WHERE ' . $where : '';
$pdoStatement = $this->pdo->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$execRet = $pdoStatement->execute($mapData);
return $execRet ? ($returnRowCount ? $pdoStatement->rowCount() : $execRet) : false;
} else {
return false;
}
}
public function delete($tableName, $condition, $mapData = array()) {
if(!empty($tableName) && $condition){
$sql = 'DELETE FROM ' . $tableName;
list($where, $mapData) = $this->FDCondition($condition, $mapData);
$sql .= $where ? ' WHERE ' . $where : '';
$pdoStatement = $this->pdo->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$execRet = $pdoStatement->execute($mapData);
return $execRet;
}
}
测试文件test.php
//PDO操作类-测试PHP示例
//author http://www.lai18.com
header("Content-type: text/html; charset=utf-8");
define('APP_DIR', dirname(__FILE__));
if (function_exists('spl_autoload_register')) {
spl_autoload_register('autoClass');
} else {
function __auto_load($className){
autoClass($className);
}
}
function autoClass($className){
try{
require_once APP_DIR.'/class/'.$className.'.php';
} catch (Exception $e) {
die('Error:' . $e->getMessage() . '
');
}
}
$DB = new DB();
//插入
$inData['a'] = rand(1, 100);
$inData['b'] = rand(1, 1000);
$inData['c'] = rand(1,200) . '.' . rand(1,100);
$ret = $DB->insert('a', $inData);
echo '插入' . ($ret ? '成功' : '失败') . '
';
//更新
$upConData['a'] = 100;
$upConJudge['a'] = '<';
$upConData['b'] = 30;
$upConJudge['b'] = '>';
list($upConStr, $mapUpConData) = $DB->FDField('b', 200, '<', 'gt');
$condition = array(
'str' => $upConStr,
'data' => $upConData,
'judge' => $upConJudge,
'link' => 'and'
);
$upData['a'] = rand(1, 10);
$upData['b'] = 1;
$upData['c'] = 1.00;
$changeRows = $DB->update('a', $upData, $condition, $mapUpConData);
echo '更新行数:' . (int) $changeRows . '
';
//删除
$delVal = rand(1, 10);
list($delCon, $mapDelCon) = $DB->FDField('a', $delVal);
$delRet = $DB->delete('a', $delCon, $mapDelCon);
echo '删除a=' . $delVal . ($delRet ? '成功' : '失败') . '
';
//查询
$data['a'] = '10';
$judge['a'] = '>';
$data['b'] = '400';
$judge['b'] = '<';
list($conSql, $mapConData) = $DB->FDFields($data, 'an