MYSQL实现主从insert和query分开操作
短信预约 -IT技能 免费直播动态提醒
<?php
class gMysql {
public $conn;// 当前使用的连接
public $arrSql;
// 当前连接ID
public $m_link_id = null; // 主库连接
public $s_link_id = null; // 从库连接
// 是否多库
public $multi_server = false;
// 数据库连接参数配置
public $dbConfig = array ();
public $dbCurrent = array ();
public function __construct($dbConfig)
{
$this->dbConfig = $dbConfig;
$this->multi_server = empty ( $this->dbConfig['slave'] ) ? false : true;
}
public function connect ($db) {
$this->dbCurrent = $db;
$linkfunction = ( TRUE == $db['persistent'] ) ? 'mysql_pconnect' : 'mysql_connect';
$this->conn = $linkfunction ( $db ['host'], $db ['username'], $db ['password']);
if (! $this->conn) {
return false;
}
$re = mysql_select_db($db['dbname'], $this->conn);
if(!$re) {
return false;
}
mysql_query ( "SET NAMES '" . $db ['charset'] . "'", $this->conn );
}
public function initConnect ($master = true) {
if ($master || !$this->multi_server) {
if($this->m_link_id){
$this->conn = $this->m_link_id;
$this->ping($master);
} else {
$this->connect ( $this->dbConfig ['master'] );
$this->m_link_id = $this->conn;
}
} else {
if($this->s_link_id){
$this->conn = $this->s_link_id;
$this->ping($master);
} else {
$rand = rand(0, count($this->dbConfig ['slave']) - 1);
$this->connect ( $this->dbConfig ['slave'][$rand] );
$this->s_link_id = $this->conn;
}
}
}
public function getArray($sql)
{
if( ! $result = $this->query($sql) )return FALSE;
if( ! mysql_num_rows($result) ) return FALSE;
$rows = array();
while($rows[] = mysql_fetch_array($result,MYSQL_ASSOC)){}
mysql_free_result($result);
array_pop($rows);
return $rows;
}
public function newinsertid()
{
return mysql_insert_id($this->conn);
}
public function setlimit($sql, $limit)
{
return $sql. " LIMIT {$limit}";
}
public function exec($sql)
{
$this->arrSql[] = $sql;
$this->initConnect ( true );
return mysql_query($sql, $this->conn);
}
public function query ($sql, $master = false) {
$this->arrSql[] = $sql;
$this->initConnect ( $master );
return mysql_query($sql, $this->conn);
}
public function affected_rows()
{
return mysql_affected_rows($this->conn);
}
public function getTable($tbl_name)
{
return $this->getArray("DESCRIBE {$tbl_name}");
}
//防止mysql gone away
public function ping($master) {
if(!@mysql_ping($this->conn)){
@mysql_close($this->conn); //注意:一定要先执行数据库关闭,这是关键
if($master || !$this->multi_server) {
unset($this->m_link_id);
} else {
unset($this->s_link_id);
}
$this->initConnect($master);
}
}
public function __val_escape($value) {
if(is_null($value))return 'NULL';
if(is_bool($value))return $value ? 1 : 0;
if(is_int($value))return (int)$value;
if(is_float($value))return (float)$value;
if(@get_magic_quotes_gpc())$value = stripslashes($value);
$this->conn || $this->initConnect();
return '\''.mysql_real_escape_string($value, $this->conn).'\'';
}
public function escape($value) {
if(is_null($value))return 'NULL';
if(is_bool($value))return $value ? 1 : 0;
if(is_int($value))return (int)$value;
if(is_float($value))return (float)$value;
if(@get_magic_quotes_gpc())$value = stripslashes($value);
$this->conn || $this->initConnect();
return mysql_real_escape_string($value, $this->conn);
}
public function __destruct()
{
if( TRUE != @$this->dbCurrent['persistent'] )@mysql_close($this->conn);
}
}
重点在 initConnect($master)方法里,这里决定加载的配置文件中是连接到主库还是从库
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341