<?php
class
mysql_rw_php {
var
$querynum
= 0;
var
$link
= null;
var
$charset
;
var
$cur_db
=
''
;
var
$ro_exist
= false;
var
$link_ro
= null;
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'
);
}
}
if
(!
$this
->link && !
$halt
)
return
false;
if
(
$this
->link_rw == null)
$this
->link_rw =
$this
->link;
if
(
$this
->version() >
'4.1'
) {
if
(
$this
->charset) {
@Mysql_query(
"SET character_set_connection=$this->charset, character_set_results=$this->charset, character_set_client=binary"
,
$this
->link);
}
if
(
$this
->version() >
'5.0.1'
) {
@Mysql_query(
"SET sql_mode=''"
,
$this
->link);
}
}
if
(
$dbname
) {
$this
->select_db(
$dbname
);
}
}
function
connect_ro(
$dbhost
,
$dbuser
,
$dbpw
,
$dbname
=
''
,
$pconnect
= 0){
if
(
$this
->link_rw == null)
$this
->link_rw =
$this
->link;
$this
->link = null;
$this
->connect(
$dbhost
,
$dbuser
,
$dbpw
,
$dbname
,
$pconnect
, false);
if
(
$this
->link){
$this
->ro_exist = true;
$this
->link_ro =
$this
->link;
if
(
$this
->cur_db){
@mysql_select_db(
$this
->cur_db,
$this
->link_ro);
}
}
else
{
$this
->link = &
$this
->link_rw;
}
}
function
set_ro_list(
$ro_list
){
if
(
is_array
(
$ro_list
)){
$link_ro
=
$ro_list
[
array_rand
(
$ro_list
)];
$this
->connect_ro(
$link_ro
[
'dbhost'
],
$link_ro
[
'dbuser'
],
$link_ro
[
'dbpw'
]);
}
}
function
select_db(
$dbname
) {
$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;
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
"<div style=\"position:absolute;font-size:11px;font-family:verdana,arial;background:#EBEBEB;padding:0.5em;\">
<b>MySQL Error</b><br>
<b>Message</b>:
$message
<br>
<b>SQL</b>:
$sql
<br>
<b>Error</b>:
$dberror
<br>
<b>Errno.</b>:
$dberrno
<br>
</div>";
exit
();
}
}
?>