phpengine/engine/lib/Mysql.class.php

138 lines
4.3 KiB
PHP
Raw Normal View History

2014-03-26 06:18:20 +00:00
<?php
class Mysql{
private $master;
private $slave;
public $do_replication = false;
function __construct( $master ){
$this->master = $master;
}
private function connect( $is_master = true ){
if( $is_master ) $dbInfo = $this->master;
else $dbInfo = $this->slave;
2014-04-11 05:09:33 +00:00
if( !$db = mysqli_connect( $dbInfo['host'] , $dbInfo['user'] , $dbInfo['passwd'] ) ){
2014-03-26 06:18:20 +00:00
die('can\'t connect to mysql ' . $ $dbInfo['host'] );
}else{
2014-04-11 05:09:33 +00:00
// mysqli_query( "set names 'utf8'" , $db );
2014-04-11 05:11:51 +00:00
mysqli_query( $db , "set names 'utf8'" );
2014-03-26 06:18:20 +00:00
}
//echo 'connect to: '. $dbInfo['host'].'at db:'.$dbInfo['dbname'].'<br>';
2014-04-11 05:09:33 +00:00
mysqli_select_db( $db , $dbInfo['dbname'] );
2014-03-26 06:18:20 +00:00
return $db;
}
private function dbRead(){
if( isset( $this->dbRead ) ){
2014-04-11 05:09:33 +00:00
mysqli_ping( $this->dbRead );
2014-03-26 06:18:20 +00:00
return $this->dbRead;
}else{
if( !$this->do_replication ) return $this->dbWrite();
else {
$this->dbRead = $this->connect( false );
return $this->dbRead;
}
}
}
private function dbWrite(){
if( isset( $this->dbWrite ) ){
2014-04-11 05:09:33 +00:00
mysqli_ping( $this->dbWrite );
2014-03-26 06:18:20 +00:00
return $this->dbWrite;
}else{
$this->dbWrite = $this->connect( true );
return $this->dbWrite;
}
}
public function setSlave( $slave ){
$this->slave['host'] = $slave['host'];
$this->slave['user'] = isset($slave['user']) ? $slave['user'] : $this->master['user'];
$this->slave['passwd'] = isset($slave['passwd']) ? $slave['passwd'] : $this->master['passwd'];
$this->slave['dbname'] = isset($slave['dbname']) ? $slave['dbname'] : $this->master['dbname'];
$this->do_replication = true;
}
2014-04-11 05:09:33 +00:00
public function saveError( $dblink ) {
//$GLOBALS['MYSQL_LAST_ERROR'] = mysqli_error($dblink);
//$GLOBALS['MYSQL_LAST_ERRNO'] = mysqli_errno($dblink);
if( mysqli_errno($dblink) ){
print_r( mysqli_error($dblink) );
2014-03-26 06:18:20 +00:00
}
2014-04-11 05:09:33 +00:00
2014-03-26 06:18:20 +00:00
}
public function runSql( $sql ) {
2014-04-11 05:09:33 +00:00
$ret = mysqli_query( $this->dbWrite() , $sql );
$this->saveError( $this->dbWrite() );
2014-03-26 06:18:20 +00:00
return $ret;
}
public function getData( $sql , $key = NULL ){
$GLOBALS['MYSQL_LAST_SQL'] = $sql;
$data = Array();
$i = 0;
2014-04-11 05:09:33 +00:00
$result = mysqli_query( $this->do_replication ? $this->dbRead() : $this->dbWrite() , $sql );
2014-03-26 06:18:20 +00:00
2014-04-11 05:09:33 +00:00
$this->saveError( $this->do_replication ? $this->dbRead() : $this->dbWrite() );
2014-03-26 06:18:20 +00:00
2014-04-11 05:09:33 +00:00
while( $Array = mysqli_fetch_array($result, MYSQL_ASSOC ) ){
2014-03-26 06:18:20 +00:00
if( $key && isset( $Array[$key] ) ){
$data[$Array[$key]] = $Array;
}else{
$data[$i++] = $Array;
}
}
/*
if( mysql_errno() != 0 )
echo mysql_error() .' ' . $sql;
*/
2014-04-11 05:09:33 +00:00
//mysqli_free_result($result);
2014-03-26 06:18:20 +00:00
if( count( $data ) > 0 )
return $data;
else
return false;
}
public function getLine( $sql ){
$data = $this->getData( $sql );
return @reset($data);
}
public function getVar( $sql ){
$data = $this->getLine( $sql );
return $data[ @reset(@array_keys( $data )) ];
}
public function lastId(){
2014-04-11 05:09:33 +00:00
$result = mysqli_insert_id( $this->dbWrite() );
return $result;
2014-03-26 06:18:20 +00:00
}
public function closeDb(){
if( isset( $this->dbRead ) ){
2014-04-11 05:09:33 +00:00
@mysqli_close( $this->dbRead );
2014-03-26 06:18:20 +00:00
unset( $this->dbRead );
}
if( isset( $this->dbWrite ) ){
2014-04-11 05:09:33 +00:00
@mysqli_close( $this->dbWrite );
2014-03-26 06:18:20 +00:00
unset( $this->dbWrite );
}
}
public function escape( $str ){
if( isset($this->dbRead)) $db = $this->dbRead ;
elseif( isset($this->dbWrite) ) $db = $this->dbWrite;
else $db = $this->dbRead();
2014-04-11 05:09:33 +00:00
return mysqli_real_escape_string( $db , $str );
2014-03-26 06:18:20 +00:00
}
public function errno(){
return $GLOBALS['MYSQL_LAST_ERRNO'];
}
public function error(){
return $GLOBALS['MYSQL_LAST_ERROR'];
}
}