<?php
Class core{
/* Array inheritance */
static function InHerit ($arr _orgin, $arr _output) {
return Array_merge ($arr _orgin, $arr _output);
}
/* Print error */
static function ThrowError ($errmsg) {
echo ' <p>error: '. $ ErrMsg. ' </p> ';
exit ();
}
}
<?php
class db{
private $result = Array ();
Private $connector = Array ();
Private $configs = Array ();
Private $active = 0;
private $default _config = Array (
' DbType ' => ' MySQL ',
' index ' => 0, ' user ' => ' root ', ' pwd ' => ' , ' host ' => ' localhost ', ' Port ' => 3306, ' charset ' => ' utf8 ', ' dbname ' => null
);
* Initialize */
public Function __construct ($config = Array ()) {
if ($config) $this->connect ($config);
}
Public Function __destruct () {
foreach ($this->connector as $index => $connect)
$this->{' _ '. $this->configs[$index [' DbType ']. ' _close '} ($connect);
}
Private Function _mysql_close ($connect) {
Mysqli_close ($connect);
}
/* Select Connection * *
Public Function Selectconnect ($index) {
return Isset ($this->connector[$index]) && (($this->active = $index) | | true);
}
/* Establish a connection * *
Public function Connect ($config) {
(!isset ($config [' Index '])] && $config [' index '] = $this->default_config[' index ']++;
$config = Core::inherit ($this->default_config, $config);
!in_array ($config [' DbType '],array (' MySQL ')) && core::throwerror (' Unsupported database type ');
Extract ($config);
$this->configs[$index] = $config;
$this->{' _ '. $config [' DbType ']. ' _connect '} ($user, $pwd, $host, $dbname, $charset, $index, $port);
}
Private Function _mysql_connect ($user = ' root ', $pwd = ', $host = ' localhost ', $dbname = null, $charset = ' UTF8 ', $index = 0, $port = 3306) {
$this->connector[$index] = Mysqli_connect ($host, $user, $pwd, NULL, $port) or Core::throwerror (Mysql_error ());
$this->active = $index;
if ($dbname) $this->selectdb ($dbname, $charset);
}
/* Get Current Connection * *
Private Function _getconnect () {
return $this->connector[$this->active];
}
/* Get the database type used by the current connection with automatic concatenation of parameters for the function name * *
Private Function _getdbtypefunc ($funcname = null) {
$dbtype = $this->configs[$this->active][' dbtype ');
return $funcname? ' _ '. $dbtype. ' _ '. $funcname: $dbtype;
}
/* Select Database * *
Public Function Selectdb ($dbname, $charset) {
mysqli_select_db ($this->_getconnect (), $dbname) or Core::throwerror (Mysql_error ());
$this->_mysql_query (' Set names '. $charset);
}
/* EXECUTE statement * *
Private Function _mysql_query ($sql) {
$result = Mysqli_query ($this->_getconnect (), $sql) or Core::throwerror (Mysql_error ());
return $result;
}
Private Function _mysql_bind_by_name ($sql, $SQLV) {
$sql _param = Array ();
$getparam = $getparam 2 = ' \w+? '; /$getparam = Array_keys ($SQLV); Implode (' | ', $getparam);
Preg_match_all ('/:('. $getparam. ') \b/iu ', $sql, $getparam) or core::throwerror (' parameter binding error ');
$getparam = $getparam [1];
$getparam = Array_flip ($getparam);
Count ($getparam)!= count ($sqlv) | | Array_diff_key ($getparam, $SQLV) && core::throwerror (' parameter mismatch ');
$sql = preg_replace ('/:('. $getparam 2. ') \b/iu ', '? ', $sql);
$SQLV = Array_merge ($getparam, $SQLV);
Unset ($getparam 2);
Unset ($getparam);
$stmt = Mysqli_prepare ($this->_getconnect (), $sql) or core::throwerror (' Wrong sql: '. $sql);
$ptype = ';
$bindparam = Array ($stmt, ');
foreach ($sqlv as $k => $v) {
$ptype. = $this->_getparamtype ($k);
$bindparam [] = $v;
}
$bindparam [1] = $ptype;
Call_user_func_array (' Mysqli_stmt_bind_param ', $bindparam);
return $this->_mysql_stmt_exec ($stmt, $sql);
}
Private Function _mysql_bind_in_sort () {
$argus = Func_get_args ();
$sql = Array_shift ($argus);
$stmt = Mysqli_prepare ($this->_getconnect (), $sql) or core::throwerror (' Wrong sql: '. $sql);
$pcount = Mysqli_stmt_param_count ($stmt);
$pcount!= count ($argus) && core::throwerror (' parameter mismatch ');
$ptype = str_repeat (' s ', $pcount);
Array_splice ($argus, 0, 0, Array ($stmt, $ptype));
Call_user_func_array (' Mysqli_stmt_bind_param ', $argus);
return $this->_mysql_stmt_exec ($stmt, $sql);
}
Private Function _mysql_stmt_exec ($stmt, $sql) {
Mysqli_stmt_execute ($stmt);
if ($this->isselect ($sql)) {///return value binding
Mysqli_stmt_bind_result ($stmt, $a);
while ($stmt->fetch ()) {
Print_r ($a);
Echo ' <br/> ';
}
}
$stmt->close ();
}
/* Use different functions according to the Parameter form * *
Private Function _mysql_iquery ($sql, $sqlv = Array ()) {
if ($SQLV) {
if (Is_array ($SQLV))
return $this->_mysql_bind_by_name ($sql, $SQLV);
else{
$argus = Func_get_args ();
return Call_user_func_array (Array ($this, ' _mysql_bind_in_sort '), $argus);
}
}else{
return $this->_mysql_query ($sql, $SQLV);
}
}
/* Distinguish data types based on key prefix prefixes * *
Private Function _getparamtype ($key) {
$r = ' s ';
/* First Letter lowercase second letter uppercase The first letter is the pattern prefix * *
$mode = Array (
' I ' => ' I ', ' s ' => ' s ', ' d ' => ' d ', ' B ' => ' B '
);
if (strlen ($key) >= 2 && $key [0] = = strtolower ($key [0]) && $key [1] = = Strtoupper ($key [1]) && I N_array ($key [0], $mode))
$r = $mode [$key [0]];
return $r;
}
/* Analysis SQL statement judgment behavior * *
Private Function _cmdtype ($sql) {
Return substr (Strtolower ($sql), 0,strpos ($sql, '));
}
/* Parse Whether SQL statement is a SELECT statement * *
Private Function Isselect ($sql) {
Return ' SELECT ' = = $this->_cmdtype ($sql);
}
Public Function query ($sql, $SQLV = Array ()) {
$func = $this->_getdbtypefunc (' IQuery ');
$argus = Func_get_args ();
$argus [0] = Trim ($argus [0]);
return Call_user_func_array (Array ($this, $func), $argus);
}
}
?>
<?php
$d = Array (
' dbname ' => ' test ',
' CharSet ' => ' latin1 '
);
$db = new db ($d);
Array-Mode binding parameters
$SQLV = Array (' iId1 ' => ', ' Id2 ' => 30);
$r = $db-> query ("SELECT ID from toselect where ID >: IID1 and ID <: ID2", $SQLV);
Standard way to bind parameters
$r = $db-> query ("Select content from Toselect where ID >?", 13);
Var_dump ($R);
?>