change mysql to mysqli

This commit is contained in:
October 2014-04-11 13:09:33 +08:00
parent 4fe995d57f
commit 3bfc8c8b43

View File

@ -9,20 +9,20 @@ class Mysql{
private function connect( $is_master = true ){ private function connect( $is_master = true ){
if( $is_master ) $dbInfo = $this->master; if( $is_master ) $dbInfo = $this->master;
else $dbInfo = $this->slave; else $dbInfo = $this->slave;
if( !$db = mysql_connect( $dbInfo['host'] , $dbInfo['user'] , $dbInfo['passwd'] ) ){ if( !$db = mysqli_connect( $dbInfo['host'] , $dbInfo['user'] , $dbInfo['passwd'] ) ){
die('can\'t connect to mysql ' . $ $dbInfo['host'] ); die('can\'t connect to mysql ' . $ $dbInfo['host'] );
}else{ }else{
// mysql_query( "set names 'utf8'" , $db ); // mysqli_query( "set names 'utf8'" , $db );
mysql_query( "set names 'utf8mb4'" , $db ); mysqli_query( $db , "set names 'utf8mb4'" );
} }
//echo 'connect to: '. $dbInfo['host'].'at db:'.$dbInfo['dbname'].'<br>'; //echo 'connect to: '. $dbInfo['host'].'at db:'.$dbInfo['dbname'].'<br>';
mysql_select_db( $dbInfo['dbname'] , $db ); mysqli_select_db( $db , $dbInfo['dbname'] );
return $db; return $db;
} }
private function dbRead(){ private function dbRead(){
if( isset( $this->dbRead ) ){ if( isset( $this->dbRead ) ){
mysql_ping( $this->dbRead ); mysqli_ping( $this->dbRead );
return $this->dbRead; return $this->dbRead;
}else{ }else{
if( !$this->do_replication ) return $this->dbWrite(); if( !$this->do_replication ) return $this->dbWrite();
@ -35,7 +35,7 @@ class Mysql{
private function dbWrite(){ private function dbWrite(){
if( isset( $this->dbWrite ) ){ if( isset( $this->dbWrite ) ){
mysql_ping( $this->dbWrite ); mysqli_ping( $this->dbWrite );
return $this->dbWrite; return $this->dbWrite;
}else{ }else{
$this->dbWrite = $this->connect( true ); $this->dbWrite = $this->connect( true );
@ -50,29 +50,30 @@ class Mysql{
$this->slave['dbname'] = isset($slave['dbname']) ? $slave['dbname'] : $this->master['dbname']; $this->slave['dbname'] = isset($slave['dbname']) ? $slave['dbname'] : $this->master['dbname'];
$this->do_replication = true; $this->do_replication = true;
} }
public function saveError() { public function saveError( $dblink ) {
//$GLOBALS['MYSQL_LAST_ERROR'] = mysql_error(); //$GLOBALS['MYSQL_LAST_ERROR'] = mysqli_error($dblink);
//$GLOBALS['MYSQL_LAST_ERRNO'] = mysql_errno(); //$GLOBALS['MYSQL_LAST_ERRNO'] = mysqli_errno($dblink);
if( mysql_errno() ){ if( mysqli_errno($dblink) ){
print_r( mysql_error() ); print_r( mysqli_error($dblink) );
} }
} }
public function runSql( $sql ) { public function runSql( $sql ) {
$ret = mysql_query( $sql , $this->dbWrite() ); $ret = mysqli_query( $this->dbWrite() , $sql );
$this->saveError(); $this->saveError( $this->dbWrite() );
return $ret; return $ret;
} }
public function getData( $sql , $key = NULL ){ public function getData( $sql , $key = NULL ){
$GLOBALS['MYSQL_LAST_SQL'] = $sql; $GLOBALS['MYSQL_LAST_SQL'] = $sql;
$data = Array(); $data = Array();
$i = 0; $i = 0;
$result = mysql_query( $sql , $this->do_replication ? $this->dbRead() : $this->dbWrite() ); $result = mysqli_query( $this->do_replication ? $this->dbRead() : $this->dbWrite() , $sql );
$this->saveError(); $this->saveError( $this->do_replication ? $this->dbRead() : $this->dbWrite() );
while( $Array = mysql_fetch_array($result, MYSQL_ASSOC ) ){ while( $Array = mysqli_fetch_array($result, MYSQL_ASSOC ) ){
if( $key && isset( $Array[$key] ) ){ if( $key && isset( $Array[$key] ) ){
$data[$Array[$key]] = $Array; $data[$Array[$key]] = $Array;
}else{ }else{
@ -85,7 +86,7 @@ class Mysql{
echo mysql_error() .' ' . $sql; echo mysql_error() .' ' . $sql;
*/ */
mysql_free_result($result); //mysqli_free_result($result);
if( count( $data ) > 0 ) if( count( $data ) > 0 )
return $data; return $data;
@ -104,17 +105,17 @@ class Mysql{
} }
public function lastId(){ public function lastId(){
$result = mysql_query( "SELECT LAST_INSERT_ID()" , $this->dbWrite() ); $result = mysqli_insert_id( $this->dbWrite() );
return reset( mysql_fetch_array( $result, MYSQL_ASSOC ) ); return $result;
} }
public function closeDb(){ public function closeDb(){
if( isset( $this->dbRead ) ){ if( isset( $this->dbRead ) ){
@mysql_close( $this->dbRead ); @mysqli_close( $this->dbRead );
unset( $this->dbRead ); unset( $this->dbRead );
} }
if( isset( $this->dbWrite ) ){ if( isset( $this->dbWrite ) ){
@mysql_close( $this->dbWrite ); @mysqli_close( $this->dbWrite );
unset( $this->dbWrite ); unset( $this->dbWrite );
} }
} }
@ -124,7 +125,7 @@ class Mysql{
elseif( isset($this->dbWrite) ) $db = $this->dbWrite; elseif( isset($this->dbWrite) ) $db = $this->dbWrite;
else $db = $this->dbRead(); else $db = $this->dbRead();
return mysql_real_escape_string( $str , $db ); return mysqli_real_escape_string( $db , $str );
} }
public function errno(){ public function errno(){