php mysql通用类_PHP连接MYSQL数据库通用类_PHP教程

Class Dirver{

//连接数据库

//link database

function DBLink($dbhost='localhost', $dbuser='root', $password='', $dbname='', $pconnect=0){

$this->LinkID[$this->Line] = ($pconnect == 1)? @mysql_pconnect($dbhost, $dbuser, $password) : @mysql_connect($dbhost, $dbuser, $password) or die("Connect to MySQL ($dbhost,$dbuser) failed");

//选择连接数据库

//choose to link database

@mysql_select_db($dbname, $this->LinkID[$this->Line]) or die('Cannot use database '.$dbname);

return $this->LinkID[$this->Line];

}

//查询语句

//view qurry

function query($query,$limit='') {

$this -> nums ++;

//检测如果有限制数据集则处理

//test if there is finite data,then function

if($limit>0){

$query = $query.' LIMIT '.$limit;

}

$this-> Lists[$this->Line][] = $query;

$querys = mysql_query($query,$this->LinkID[$this->Line]);

if(!$querys){

$this->DB_Error($query);

}

return $querys;

}

//返回数组资料

//back to array info

function fetch_array($query) {

return @mysql_fetch_array($query, MYSQL_ASSOC);

}

//返回数组资料

//back to array info

function result($query) {

return @mysql_result($query,$this->LinkID[$this->Line]);

}

//返回数组资料行

//back to row info

function rows($query) {

return $this->fetch_array($this->query($query));

}

//返回数组行

//back to numrows

function nums($query) {

return $this->num_rows($this->query($query));

}

//取得返回列的数目

//fetch the numbers backing out

function num_rows($query){

return @mysql_num_rows($query);

}

//返回单列的各字段

//return to every field of single row

function fetch_row($query) {

return @mysql_fetch_row($query);

}

//返回最后一次使用 INSERT 指令的 ID

//return to inserted ID used last time

function insert_id(){

return @mysql_insert_id($this->LinkID[$this->Line]);

}

//关闭当前数据库连接

//close current database link

function close(){

return @mysql_close($this->LinkID[$this->Line]);

}

//检测mysql版本

//test mysql version

function version(){

$query = @mysql_query("SELECT VERSION()",$this->LinkID[$this->Line]);

return @mysql_result($query, 0);

}

//返回友情提示信息 更多http://www.52mvc.com

//return to kindly note

function DB_Error_t($query=''){

}

function DB_Error($query=''){

global $PHP_SELF;

//出错语句提示

//error sentence

$errors = preg_replace("/'(.+?)'/is"," '//1' ",mysql_error());

$charset ='';

//提示语言

//language noted

$lang = Array('This SQL Error Info!', 'Error Script:', 'Present time:',

'Http Host:', 'Server Name:', 'Server Software:',

'Host IP Address:', 'Remote User Agent:', 'Current File:',

'Current Line:', 'Line.', 'The Error number:',

'The specific Error was:', 'SQL Query :', 'Not discover whateverly SQL Sentence !');

//时间处理

//time

$nowdate = date('Y-m-d H:i A');

$errors = preg_replace("/'(.+?)'/is","'//1'",mysql_error());

//检测是否有语句

//test if there is any sentence.

if($query==''){

$query = $lang[14];

}

echo "

$lang[0]

$lang[0]

$lang[2]

$nowdate

$lang[3]

".$_SERVER['HTTP_HOST']."

$lang[4]

".$_SERVER['SERVER_NAME']."

$lang[5]

".$_SERVER['SERVER_SOFTWARE']."

$lang[6]

".$_SERVER['REMOTE_ADDR']."

$lang[7]

".$_SERVER['HTTP_USER_AGENT'].";

$lang[11]

".mysql_errno()."

$lang[12]

$errors

$lang[13]

$query

";

exit;

}

}

?>

PHP连接MYSQL数据库类

include "mysql.php";

$db = new Dirver();

$db->DBLink($dbhost='localhost', $dbuser='root', $password='password', $dbname='name', $pconnect=0);

?>

PHP连接MYSQL数据库类

mysql.php

Class Dirver{

//连接数据库

//link database

function DBLink($dbhost='localhost', $dbuser='root', $password='', $dbname='', $pconnect=0){

$this->LinkID[$this->Line] = ($pconnect == 1)? @mysql_pconnect($dbhost, $dbuser, $password) : @mysql_connect($dbhost, $dbuser, $password) or die("Connect to MySQL ($dbhost,$dbuser) failed");

//选择连接数据库

//choose to link database

@mysql_select_db($dbname, $this->LinkID[$this->Line]) or die('Cannot use database '.$dbname);

return $this->LinkID[$this->Line];

}

//查询语句

//view qurry

function query($query,$limit='') {

$this -> nums ++;

//检测如果有限制数据集则处理

//test if there is finite data,then function

if($limit>0){

$query = $query.' LIMIT '.$limit;

}

$this-> Lists[$this->Line][] = $query;

$querys = mysql_query($query,$this->LinkID[$this->Line]);

if(!$querys){

$this->DB_Error($query);

}

return $querys;

}

//返回数组资料

//back to array info

function fetch_array($query) {

return @mysql_fetch_array($query, MYSQL_ASSOC);

}

//返回数组资料

//back to array info

function result($query) {

return @mysql_result($query,$this->LinkID[$this->Line]);

}

//返回数组资料行

//back to row info

function rows($query) {

return $this->fetch_array($this->query($query));

}

//返回数组行

//back to numrows

function nums($query) {

return $this->num_rows($this->query($query));

}

//取得返回列的数目

//fetch the numbers backing out

function num_rows($query){

return @mysql_num_rows($query);

}

//返回单列的各字段

//return to every field of single row

function fetch_row($query) {

return @mysql_fetch_row($query);

}

//返回最后一次使用 INSERT 指令的 ID

//return to inserted ID used last time

function insert_id(){

return @mysql_insert_id($this->LinkID[$this->Line]);

}

//关闭当前数据库连接

//close current database link

function close(){

return @mysql_close($this->LinkID[$this->Line]);

}

//检测mysql版本

//test mysql version

function version(){

$query = @mysql_query("SELECT VERSION()",$this->LinkID[$this->Line]);

return @mysql_result($query, 0);

}

//返回友情提示信息

//return to kindly note

function DB_Error_t($query=''){

}

function DB_Error($query=''){

global $PHP_SELF;

//出错语句提示

//error sentence

$errors = preg_replace("/'(.+?)'/is"," '//1' ",mysql_error());

$charset ='';

//提示语言

//language noted

$lang = Array('This SQL Error Info!', 'Error Script:', 'Present time:',

'Http Host:', 'Server Name:', 'Server Software:',

'Host IP Address:', 'Remote User Agent:', 'Current File:',

'Current Line:', 'Line.', 'The Error number:',

'The specific Error was:', 'SQL Query :', 'Not discover whateverly SQL Sentence !');

//时间处理

//time

$nowdate = date('Y-m-d H:i A');

$errors = preg_replace("/'(.+?)'/is","'//1'",mysql_error());

//检测是否有语句

//test if there is any sentence.

if($query==''){

$query = $lang[14];

}

echo "

$lang[0]

$lang[0]

$lang[2]

$nowdate

$lang[3]

".$_SERVER['HTTP_HOST']."

$lang[4]

".$_SERVER['SERVER_NAME']."

$lang[5]

".$_SERVER['SERVER_SOFTWARE']."

$lang[6]

".$_SERVER['REMOTE_ADDR']."

$lang[7]

".$_SERVER['HTTP_USER_AGENT'].";

$lang[11]

".mysql_errno()."

$lang[12]

$errors

$lang[13]

$query

";

exit;

}

}

?>

http://www.bkjia.com/PHPjc/477775.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477775.htmlTechArticle?php include mysql.php; $db = new Dirver(); $db-DBLink($dbhost=localhost, $dbuser=root, $password=password, $dbname=name, $pconnect=0); ? PHP连接MYSQL数据库类 mysql.php ?php...

f68f2add0b68e4f9810432fce46917b7.png

相关文章

相关视频

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值