PHP MySQL database connection class program code

Source: Internet
Author: User
The code is as follows Copy Code

Class cls_mysql{
var $querynum = 0;
var $link;
var $histories;

var $dbhost;
var $dbuser;
var $dbpw;
var $dbcharset;
var $pconnect;
var $tablepre;
var $time;

var $goneaway = 5;

Function Connect ($dbhost, $dbuser, $DBPW, $dbname = ', $dbcharset = ', $pconnect = 0, $tablepre = ', $time = 0) {
$this->dbhost = $dbhost;
$this->dbuser = $dbuser;
$this->DBPW = $DBPW;
$this->dbname = $dbname;
$this->dbcharset = $dbcharset;
$this->pconnect = $pconnect;
$this->tablepre = $tablepre;
$this->time = $time;

if ($pconnect) {
if (! $this->link = Mysql_pconnect ($dbhost, $dbuser, $DBPW)) {
$this->halt (' Can not connect to MySQL server ');
}
} else {
if (! $this->link = mysql_connect ($dbhost, $dbuser, $DBPW)) {
$this->halt (' Can not connect to MySQL server ');
}
}

if ($this->version () > ' 4.1 ') {
if ($dbcharset) {
mysql_query ("Set character_set_connection=". $dbcharset. ", character_set_results=". $dbcharset. ", character_set_ Client=binary ", $this->link);
}

if ($this->version () > ' 5.0.1 ') {
mysql_query ("Set sql_mode=" ", $this->link);
}
}

if ($dbname) {
mysql_select_db ($dbname, $this->link);
}

}

function Fetch_array ($query, $result _type = Mysql_assoc) {
Return mysql_fetch_array ($query, $result _type);
}

function Result_first ($sql) {
$query = $this->query ($sql);
return $this->result ($query, 0);
}

function Fetch_first ($sql) {
$query = $this->query ($sql);
return $this->fetch_array ($query);
}

function Fetch_all ($sql, $id = ' ") {
$arr = Array ();
$query = $this->query ($sql);
while ($data = $this->fetch_array ($query)) {
$id? $arr [$data [$id]] = $data: $arr [] = $data;
}
return $arr;
}

function cache_gc () {
$this->query ("delete from {$this->tablepre}sqlcaches where expiry< $this->time");
}

function query ($sql, $type = ', $cachetime = False) {
$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++;
$this->histories[] = $sql;
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 = ') {
$error = mysql_error ();
$errorno = Mysql_errno ();
if ($errorno = = 2006 && $this->goneaway--> 0) {
$this->connect ($this->dbhost, $this->dbuser, $this-> DBPW, $this->dbname, $this->dbcharset, $this->pconnect, $this->tablepre, $this->time);
$this->query ($sql);
} else {
$s = ' <b>error:</b> '. $error. ' <br/> ';
$s. = ' <b>errno:</b> '. $errorno. ' <br/> ';
$s. = ' <b>sql:</b>: '. $sql;
exit ($s);
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.