An example MySQL PHP connection class under this MySQL database connection article, I got down from a CMS, very complete. The direct call is OK.
Class Dbmysq {
var $querynum = 0;
var $link;
var $histories;
var $time;
var $tablepre;
Function Connect ($dbhost, $dbuser, $DBPW, $dbname = ", $dbcharset, $pconnect = 0, $tablepre =", $time = 0) {
$this->time = $time;
$this->tablepre = $tablepre;
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, 1)) {
$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, & $data) {
$query = $this->query ($sql);
$data = $this->result ($query, 0);
}
function Fetch_first ($sql, & $arr) {
$query = $this->query ($sql);
$arr = $this->fetch_array ($query);
}
function Fetch_all ($sql, & $arr) {
$query = $this->query ($sql);
while ($data = $this->fetch_array ($query)) {
$arr [] = $data;
}
}
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 = ') {
Exit ('
Hint: Database error
SQL statement: '. $sql. '
Error keyword: '. Mysql_error ());
}
}
http://www.bkjia.com/PHPjc/630789.html www.bkjia.com true http://www.bkjia.com/PHPjc/630789.html techarticle An example MySQL PHP connection class under this MySQL database connection article, I got down from a CMS, very complete. The direct call is OK. A sample MySQL tutorial php tutorial Connection ...