<?php
Class pdomysql{
public static $config =array ();//Set connection parameters, configuration information
public static $link =null;//to save the connection identifier
public static $pconnect =false;//whether to turn on long connections
public static $dbVersion =null;//Save database version
public static $connected =false;//is connected successfully
public static $PDOStatement =null;//Save Pdostatement Object
public static $QUERYSTR =null;//Save the last action performed
public static $error =null;//error message
public static $lastInsertId =null;//Save the previous insert operation to produce auto_increment
public static $numRows =0;//the number of records affected by the previous operation
/**
* Connect PDO
* @param string $dbConfig
* @return Boolean
*/
Public function __construct ($dbConfig = ") {
if (!class_exists ("PDO")) {
Self::throw_exception (' Do not support PDO, please open first ');
}
if (!is_array ($dbConfig)) {
$dbConfig =array (
' Hostname ' =>db_host,
' Username ' =>db_user,
' Password ' =>db_pwd,
' Database ' =>db_name,
' Hostport ' =>db_port,
' DBMS ' =>db_type,
' DSN ' =>db_type. ': host= '. Db_host. "; Dbname= ". Db_name
);
}
if (Empty ($dbConfig [' hostname '])) self::throw_exception (' No database configuration defined, first define ');
Self:: $config = $dbConfig;
if (Empty (self:: $config [' params ')]) Self:: $config [' params ']=array ();
if (!isset (self:: $link)) {
$configs =self:: $config;
if (self:: $pconnect) {
To open a long connection and add it to the configuration array
$configs [' params '][constant ("pdo::attr_persistent")]=true;
}
try{
Self:: $link =new PDO ($configs [' DSN '], $configs [' username '], $configs [' Password '], $configs [' params ']);
}catch (Pdoexception $e) {
Self::throw_exception ($e->getmessage ());
}
if (!self:: $link) {
Self::throw_exception (' PDO connection error ');
return false;
}
Self:: $link->exec (' SET NAMES '). Db_charset);
Self:: $dbVersion =self:: $link->getattribute (Constant ("pdo::attr_server_version"));
Self:: $connected =true;
Unset ($configs);
}
}
/**
* Get all records
* @param string $sql
* @return Unknown
*/
public static function GetAll ($sql =null) {
if ($sql!=null) {
Self::query ($sql);
}
$result =self:: $PDOStatement->fetchall (Constant ("PDO::FETCH_ASSOC"));
return $result;
}
/**
* Get a record in the result set
* @param string $sql
* @return Mixed
*/
public static function GetRow ($sql =null) {
if ($sql!=null) {
Self::query ($sql);
}
$result =self:: $PDOStatement->fetch (Constant ("PDO::FETCH_ASSOC"));
return $result;
}
/**
* Find records based on primary key
* @param string $tabName
* @param int $priId
* @param string $fields
* @return Mixed
*/
public static function FindByID ($tabName, $priId, $fields = ' * ') {
$sql = ' SELECT%s from%s WHERE id=%d ';
Return Self::getrow (sprintf ($sql, Self::p arsefields ($fields), $tabName, $priId));
}
/**
* Perform normal queries
* @param unknown $tables
* @param string $where
* @param string $fields
* @param string $group
* @param string $having
* @param string $order
* @param string $limit
* @return ambigous <unknown, unknown, multitype:>
*/
public static function Find ($tables, $where =null, $fields = ' * ', $group =null, $having =null, $order =null, $limit =null) {
$sql = ' SELECT '. Self::p arsefields ($fields). ' From '. ' ". $tables. ' "'
. Self::p arsewhere ($where)
. Self::p arsegroup ($group)
. Self::p arsehaving ($having)
. Self::p arseorder ($order)
. Self::p arselimit ($limit);
Echo $sql;
$dataAll =self::getall ($sql);
return count ($dataAll) ==1? $dataAll [0]: $dataAll;
return $dataAll;
}
/**
* Actions to add records
* @param array $data
* @param string $table
* @return ambigous <boolean, unknown, number>
*/
public static function Add ($data, $table) {
$keys =array_keys ($data);
Array_walk ($keys, Array (' Pdomysql ', ' Addspecialchar '));
$fieldsStr =join (', ', $keys);
$values = "'". Join ("', '", Array_values ($data)). "'";
$sql = "INSERT into ' {$table} ' ({$fieldsStr}) VALUES ({$values})";
Echo $sql;
Return Self::execute ($sql);
}
/**
* Update record
* @param array $data
* @param string $table
* @param string $where
* @param string $order
* @param string $limit
* @return ambigous <boolean, unknown, number>
*/
public static function Update ($data, $table, $where =null, $order =null, $limit =0) {
foreach ($data as $key = = $val) {
$sets. = "'". $key. " ' = '. $val. "',";
}
$sets =rtrim ($sets, ', ');
$sql = "UPDATE {$table} SET {$sets}". Self::p arsewhere ($where). Self::p arseorder ($order). Self::p arselimit ($limit);
Echo $sql;
Return Self::execute ($sql);
}
/**
* Actions to delete records
* @param string $table
* @param string $where
* @param string $order
* @param number $limit
* @return ambigous <boolean, unknown, number>
*/
public static function Delete ($table, $where =null, $order =null, $limit =0) {
$sql = "DELETE from ' {$table} '". Self::p arsewhere ($where). Self::p arseorder ($order). Self::p arselimit ($limit);
Return Self::execute ($sql);
}
/**
*
*/
public static function truncate ($table) {
$sql = "TRUNCATE TABLE ' {$table} '";
Return Self::execute ($sql);
}
/**
* Get the last executed SQL statement
* @return boolean| Ambigous <string, string>
*/
public static function Getlastsql () {
$link =self:: $link;
if (! $link) return false;
Return self:: $queryStr;
}
/**
* Get the previous step insert operation to generate Auto_increment
* @return boolean|string
*/
public static function Getlastinsertid () {
$link =self:: $link;
if (! $link) return false;
Return self:: $lastInsertId;
}
/**
* Get the database version
* @return boolean|mixed
*/
public static function Getdbverion () {
$link =self:: $link;
if (! $link) return false;
Return self:: $dbVersion;
}
/**
* Get data tables in the database
* @return multitype:mixed
*/
public static function Showtables () {
$tables =array ();
if (Self::query ("SHOW TABLES")) {
$result =self::getall ();
foreach ($result as $key = = $val) {
$tables [$key]=current ($val);
}
}
return $tables;
}
/**
* Parse Where Condition
* @param unknown $where
* @return String
*/
public static function Parsewhere ($where) {
$WHERESTR = ";
if (is_string ($where) &&!empty ($where)) {
$WHERESTR = $where;
}
return empty ($WHERESTR)? ': ' WHERE '. $whereStr;
}
/**
* Resolve GROUP BY
* @param unknown $group
* @return String
*/
public static function Parsegroup ($group) {
$GROUPSTR = ";
if (Is_array ($group)) {
$groupStr. = ' GROUP by '. Implode (', ', $group);
}elseif (is_string ($group) &&!empty ($group)) {
$groupStr. = ' GROUP by '. $group;
}
return empty ($GROUPSTR)? ': $groupStr;
}
/**
* Two deletions by the HAVING clause for grouping results
* @param unknown $having
* @return String
*/
public static function Parsehaving ($having) {
$HAVINGSTR = ";
if (is_string ($having) &&!empty ($having)) {
$havingStr. = ' having '. $having;
}
return $havingStr;
}
/**
* Parse ORDER BY
* @param unknown $order
* @return String
*/
public static function Parseorder ($order) {
$ORDERSTR = ";
if (Is_array ($order)) {
$orderStr. = ' ORDER by '. Join (', ', $order);
}elseif (is_string ($order) &&!empty ($order)) {
$orderStr. = ' ORDER by '. $order;
}
return $orderStr;
}
/**
* Parse limit display number of bars limit
* Limit 3
* Limit 0,3
* @param unknown $limit
* @return Unknown
*/
public static function Parselimit ($limit) {
$LIMITSTR = ";
if (Is_array ($limit)) {
if (count ($limit) >1) {
$limitStr. = ' LIMIT '. $limit [0]. ', '. $limit [1];
}else{
$limitStr. = ' LIMIT '. $limit [0];
}
}elseif (is_string ($limit) &&!empty ($limit)) {
$limitStr. = ' LIMIT '. $limit;
}
return $limitStr;
}
/**
* Parse Field
* @param unknown $fields
* @return String
*/
public static function Parsefields ($fields) {
if (Is_array ($fields)) {
Array_walk ($fields, Array (' Pdomysql ', ' Addspecialchar '));
$fieldsStr =implode (', ', $fields);
}elseif (is_string ($fields) &&!empty ($fields)) {
if (Strpos ($fields, "') ===false) {
$fields =explode (', ', $fields);
Array_walk ($fields, Array (' Pdomysql ', ' Addspecialchar '));
$fieldsStr =implode (', ', $fields);
}else{
$FIELDSSTR = $fields;
}
}else{
$FIELDSSTR = ' * ';
}
return $fieldsStr;
}
/**
* By referencing fields in inverted quotation marks,
* @param unknown $value
* @return String
*/
public static function Addspecialchar (& $value) {
if ($value = = = ' * ' | | Strpos ($value, '. ')! ==false| | Strpos ($value, "')!==false) {
No handling.
}elseif (Strpos ($value, ") ===false) {
$value = ". Trim ($value). ' '
}
return $value;
}
/**
* Delete and change the number of affected records
* @param string $sql
* @return Boolean|unknown
*/
public static function Execute ($sql =null) {
$link =self:: $link;
if (! $link) return false;
Self:: $queryStr = $sql;
if (!empty (self:: $PDOStatement)) Self::free ();
$result = $link->exec (self:: $QUERYSTR);
Self::haveerrorthrowexception ();
if ($result | | $result ==0) {
Self:: $lastInsertId = $link->lastinsertid ();
Self:: $numRows = $result;
Return self:: $numRows;
}else{
return false;
}
}
/**
Releasing the result set
*/
public static function free () {
Self:: $PDOStatement =null;
}
public static function query ($sql = ') {
$link =self:: $link;
if (! $link) return false;
Determines whether there is a result set before, and if so, releases the result set
if (!empty (self:: $PDOStatement)) Self::free ();
Self:: $queryStr = $sql;
Self:: $PDOStatement = $link->prepare (self:: $QUERYSTR);
$res =self:: $PDOStatement->execute ();
Self::haveerrorthrowexception ();
return $res;
}
public static function Haveerrorthrowexception () {
$obj =empty (self:: $PDOStatement)? Self:: $link: Self:: $PDOStatement;
$arrError = $obj->errorinfo ();
Print_r ($arrError);
if ($arrError [0]!= ' 00000 ') {
Self:: $error = ' SQLSTATE: '. $arrError [0]. ' <br/>sql error: '. $arrError [2]. ' <br/>error SQL: '. Self:: $queryStr;
Self::throw_exception (self:: $error);
return false;
}
if (self:: $queryStr = = ") {
Self::throw_exception (' No Execute SQL statement ');
return false;
}
}
/**
* Custom error handling
* @param unknown $ERRMSG
*/
public static function Throw_exception ($ERRMSG) {
Echo ' <div style= width:80%;background-color: #ABCDEF; color:black;font-size:20px;padding:20px 0px; " >
'. $errMsg. '
</div> ';
}
/**
* Destroy the Connection object, close the database
*/
public static function close () {
Self:: $link =null;
}
public static function Search ($table 1, $table 2, $table 3) {
$PdoMySQL =new pdomysql ();
$sql 1= "SELECT * from". $table 1. "Where status= ' 0 '";
$sql 2= "SELECT * from". $table 2. "Where status= ' 0 '";
$sql 3= "SELECT * from". $table 3. "Where status= ' 0 '";
$stmt 1= $PdoMySQL->prepare ($sql 1);
$stmt 2= $PdoMySQL->prepare ($sql 2);
$stmt 3= $PdoMySQL->prepare ($sql 3);
$stmt 1->execute ();
$stmt 2->execute ();
$stmt 3->execute ();
echo $username. ' $row 1= $stmt 1->fetch ();
echo $row 1[0];
}
}
?>
Build a class that operates on MySQL