Need to turn on PHP PDO support, php5.1 above version support
To realize the single instantiation of database connection, there are three factors static variable, static instantiation method, private constructor dpdo.php
Class dpdo{private $DSN;
Private $DBUser;
Private $DBPWD;
Private $longLink;
Private $pdo;
Private constructors prevent direct instantiation of private function __construct ($DSN, $DBUser, $DBPwd, $longLink = False) {$this->dsn = $DSN;
$this->dbuser = $DBUser;
$this->dbpwd = $DBPWD;
$this->longlink = $longLink;
$this->connect (); }//Private null cloning function to prevent the cloned private function __clone () {}//static instantiation function returns a PDO object static public function instance ($DSN, $DBUser , $DBPWD, $longLink = False) {static $singleton = array ();//static function to store the instantiated object $singIndex = MD5 ($dsn. $DBUser. $DBPw D.
$longLink);
if (Empty ($singleton [$singIndex])) {$singleton [$singIndex] = new self ($dsn, $DBUser, $DBPwd, $longLink = false);
return $singleton [$singIndex]->pdo; Private Function Connect () {try{if ($this->longlink) {$this->pdo = new PDO ($this->dsn,
$this->dbuser, $this->dbpwd, Array (pdo::attr_persistent => true));
}else{ $this->pdo = new PDO ($this->dsn, $this->dbuser, $this->dbpwd);
} $this->pdo->query (' SET NAMES UTF-8 '); catch (Pdoexception $e) {die (' Error: '. $e->getmessage ().
' <br/> ');
}
}
}
The
is used to process field mappings, using PDO field mappings to effectively avoid SQL injection
field associative array processing, used primarily for writing and updating data, query conditions with and or or, generating an array of SQL statements and Mapping fields public function Fdfields ($data, $link = ', ', $judge = Array (),
$aliasTable = ') {$sql = ';
$mapData = Array (); foreach ($data as $key => $value) {$mapIndex = ': '. ($link!= ', '? ' C ': '). $aliasTable.
$key; $sql. = ". ($aliasTable $aliasTable. '.' : '') . '`' . $key. '` ' . ($judge [$key]? $judge [$key]: ' = '). ' ' . $mapIndex. ' ' .
$link;
$mapData [$mapIndex] = $value;
$sql = Trim ($sql, $link);
Return Array ($sql, $mapData); ///To process a single field handling public function Fdfield ($field, $value, $judge = ' = ', $preMap = ' cn ', $aliasTable = ') {$mapIndex = ':' . $preMap. $aliasTable.
$field; $sql = '. ($aliasTable $aliasTable. '.' : '') . '`' . $field. '`' . $judge.
$mapIndex;
$mapData [$mapIndex] = $value;
Return Array ($sql, $mapData);
///Using the Just method can easily produce query conditions and corresponding data array public function fdcondition ($condition, $mapData) {if (is_string ($condition)) { $where = $condition; else if (Is_array ($condition)) {if ($condition [' str ']) {if (is_string ($condition [' str '])} {$WH
ere = $condition [' str '];
else {return false;
} if (Is_array ($condition [' data ']) {$link = $condition [' link ']? $condition [' Link ']: ' and ';
List ($CONSQL, $mapConData) = $this->fdfields ($condition [' Data '], $link, $condition [' Judge ']); if ($CONSQL) {$where. = ($where? ' ' . $link: "").
$CONSQL;
$mapData = Array_merge ($mapData, $mapConData);
} return Array ($where, $mapData);
}
Additions and deletions to the specific implementation db.php
The Public function fetch ($sql, $searchData = Array (), $dataMode = Pdo::fetch_assoc, $preType = Array (pdo::attr_cursor =>
Pdo::cursor_fwdonly)) {if ($sql) {$sql. = ' Limit 1 ';
$pdoStatement = $this->pdo->prepare ($sql, $preType);
$pdoStatement->execute ($searchData);
return $data = $pdoStatement->fetch ($dataMode);
else {return false; The Public Function Fetchall ($sql, $searchData = Array (), $limit = Array (0), $dataMode = Pdo::fetch_assoc, $PR EType = Array (pdo::attr_cursor => pdo::cursor_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; The 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; The Public Function update ($tableName, $data, $condition, $mapData = Array (), $returnRowCount = True) {if (!em Pty ($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; The Public function Delete ($tableName, $condition, $mapData = Array ()) {if (!empty ($tableName) && $con
dition) {$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 file test.php
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 ($CLA
Ssname) {autoclass ($className); } function Autoclass ($className) {try{require_once app_dir. '/class/'. $className '.
PHP '; catch (Exception $e) {die (' Error: '. $e->getmessage ().
' <br/> ');
}} $DB = new DB ();
Insert $inData [' a '] = rand (1, 100);
$inData [' b '] = rand (1, 1000); $inData [' c '] = rand (1,200). '.' .
Rand (1,100);
$ret = $DB->insert (' A ', $inData); echo ' Insert '. ($ret? ' Success ': ' Failure '.
' <br/> ';
Update $upConData [' a '] = 100;
$upConJudge [' a '] = ' < ';
$upConData [' b '] = 30;
$upConJudge [' b '] = ' > ';
List ($upConStr, $mapUpConData) = $DB->fdfield (' B ', M, ' < ', ' 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 ' Update number of rows: '. (int) $changeRows.
' <br/> ';
Delete $delVal = rand (1, 10);
List ($delCon, $mapDelCon) = $DB->fdfield (' A ', $delVal);
$delRet = $DB->delete (' A ', $delCon, $mapDelCon); Echo ' Delete a= '. $delVal. ($delRet? ' Success ': ' Failure '.
' <br/> ';
Query $data [' a '] = ' 10 ';
$judge [' a '] = ' > ';
$data [' b '] = ' 400 ';
$judge [' b '] = ' < ';
List ($CONSQL, $mapConData) = $DB->fdfields ($data, ' and ', $judge); $mData = $DB->fetch (' select * from a where '. $conSql.
' ORDER by ' a ' desc ', $mapConData); Var_dump ($mData);
The above is the entire contents of this article, I hope you can enjoy.