This code is modified from uchome’s code and is processed to improve the efficiency of uchome. I have actually had this idea for a long time, but I have never done it. I believe there are people who have the same idea. If there are similar ideas, I really hope to provide relevant suggestions.
The encapsulation method is relatively simple, adding an interface extension for read-only database connection. Not using a read-only database does not affect the use of the original code. It remains to be continuously improved in the future. .
For convenience, try setting up a Google project:
http://code.google.com/p/mysql-rw-php/
I hope it will help friends in need.
/****************************************
*** mysql-rw-php version 0.1
*** code by hqlulu#gmail.com
*** http://www.aslibra.com
*** http://code.google.com/p/mysql-rw-php/
*** code modify from class_mysql.php (uchome)
****************************************/
class mysql_rw_php {
//Number of queries
var $querynum = 0;
//Current Operational database connection
var $link = null;
//Character set
var $charset;
//Current database
var $cur_db = '';
//Whether There is a valid read-only database connection
var $ro_exist = false;
//Read-only database connection
var $link_ro = null;
//Read-write database connection
var $link_rw = null;
function mysql_rw_php(){
}
function connect($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $halt = TRUE) {
if ($pconnect) {
if(!$this->link = @mysql_pconnect($dbhost, $dbuser, $dbpw)) {
$halt && $this->halt('Can not connect to MySQL server');
}
} else {
if(!$this->link = @mysql_connect($dbhost, $dbuser, $dbpw)) {
$halt && $this ->halt('Can not connect to MySQL server');
}
}
//Read-only connection failed
if(!$this->link && !$ halt) return false;
//When rw is not initialized, the first connection is rw
if($this->link_rw == null)
$this->link_rw = $ this->link;
if($this->version() > '4.1') {
if($this->charset) {
@mysql_query("SET character_set_connection=$ '5.0. 1') {
@mysql_query("SET sql_mode=''", $this->link);
;select_db($dbname);
}
}
//Connect a read-only mysql database
function connect_ro($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0){
if($this->link_rw == null)
$this->link_rw = $this->link;
$this->link = null;
//No halt error is generated
$this->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, false);
if($this->link){
//Connection successful
//echo "link ro sussess!
";
;
if($this->cur_db){
//If you have already selected a database, you need to operate it once
@mysql_select_db($this->cur_db, $this->link_ro);
}
}else{
;
}
}
//Set up a series of read-only databases and connect to one of them
function set_ro_list($ro_list){
if(is_array($ro_list)){
/ /Randomly select one of them
$link_ro = $ro_list[array_rand($ro_list)];
$this->connect_ro($link_ro['dbhost'], $link_ro['dbuser'], $link_ro[ 'dbpw']);
}
}
function select_db($dbname) {
//Operate two database connections at the same time
$this->cur_db = $dbname;
if($this->ro_exist){
@mysql_select_db($dbname, $this->link_ro);
}
return @mysql_select_db($dbname, $this->link_rw);
}
function fetch_array($query, $result_type = MYSQL_ASSOC) {
return mysql_fetch_array($query, $result_type);
}
function fetch_one_array($sql, $type = '') {
$qr = $this->query($sql, $type);
return $this->fetch_array($qr);
}
function query($sql, $type = '') {
$this->link = &$this->link_rw;
//判断是否select语句
if($this->ro_exist && preg_match ("/^(s*)select/i", $sql)){
$this->link = &$this->link_ro;
}
$func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
'mysql_unbuffered_query' : 'mysql_query';
if(!($query = $func($sql, $this->link)) && $type != 'SILENT') {
$this->halt('MySQL Query Error', $sql);
}
$this->querynum++;
return $query;
}
function affected_rows() {
return mysql_affected_rows($this->link);
}
function error() {
return (($this->link) ? mysql_error($this->link) : mysql_error());
}
function errno() {
return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
}
function result($query, $row) {
$query = @mysql_result($query, $row);
return $query;
}
function num_rows($query) {
$query = mysql_num_rows($query);
return $query;
}
function num_fields($query) {
return mysql_num_fields($query);
}
function free_result($query) {
return mysql_free_result($query);
}
function insert_id() {
return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
}
function fetch_row($query) {
$query = mysql_fetch_row($query);
return $query;
}
function fetch_fields($query) {
return mysql_fetch_field($query);
}
function version() {
return mysql_get_server_info($this->link);
}
function close() {
return mysql_close($this->link);
}
function halt($message = '', $sql = '') {
$dberror = $this->error();
$dberrno = $this->errno();
echo "
MySQL Error
Message: $message
SQL: $sql
Error: $dberror
Errno.: $dberrno
";
exit();
}
}
?>