您的位置:首页 > 编程语言 > PHP开发

PHP连接MySQL的数据库类文件【转】

2010-01-06 13:05 204 查看
<?php

/*这个基本上就是我使用的数据库类*/
//屏蔽未定义错误
error_reporting(7);

class DB_MySQL {

var $servername="localhost";
var $dbname="DBASE";
var $dbusername = "ROOT";
var $dbpassword = "";
var $conn = 0;
var $technicalemail=*@*.com';

function geterrdesc() {
$this->error = @mysql_error($this->conn);
return $this->error;
}

function geterrno() {
$this->errno = @mysql_errno($this->conn);
return $this->errno;
}

function query($query_string) {

// $this->result = mysql_db_query($this->dbname,$query_string);
$this->result = mysql_query($query_string);
if (!$this->result) {
$this->halt("SQL 无效: ".$query_string);
}

return $this->result;
}

function num_rows($queryid) {

$this->rows = mysql_num_rows($queryid);

if (empty($queryid)){
$this->halt("Query id 无效:".$queryid);
}
return $this->rows;
}

function fetch_array($queryid) {

$this->record = mysql_fetch_array($queryid);
if (empty($queryid)){
$this->halt("Query id 无效:".$queryid);
}
return $this->record;
}

function conn(){
$this->conn = mysql_connect($this->servername, $this->dbusername, $this->dbpassword) or die(mysql_error("数据库链接失败"));
return $this->conn;
}

function selectdb(){
if(!mysql_select_db($this->dbname)){
$this->halt("数据库链接失败");
}
}

function my_close() {
// mysql_close($this->conn);
mysql_close();
}

function fetch_row($queryid) {

$this->record = mysql_fetch_row($queryid);
if (empty($queryid)){
$this->halt("queryid 无效:".$queryid);
}
return $this->record;
}

function fetch_one_num($query) {

$this->result = $this->query($query);
$this->record = $this->num_rows($this->result);
if (empty($query)){
$this->halt("Query id 无效:".$query);
}
return $this->record;

}
function fetch_one_array($query) {

$this->result = $this->query($query);
$this->record = $this->fetch_array($this->result);
if (empty($query)){
$this->halt("Query id 无效:".$query);
}
return $this->record;

}

function free_result($query){
if (!mysql_free_result($query)){
$this->halt("fail to mysql_free_result");
}
}

function insert_id(){
$this->insertid = mysql_insert_id();
if (!$this->insertid){
$this->halt("fail to get mysql_insert_id");
}
return $this->insertid;
}

/*========================================================================*/
// Create an array from a multidimensional array returning formatted
// strings ready to use in an INSERT query, saves having to manually format
// the (INSERT INTO table) ('field', 'field', 'field') VALUES ('val', 'val')
/*========================================================================*/

function compile_db_insert_string($data) {

$field_names = "";
$field_values = "";

foreach ($data as $k => $v)
{
$v = preg_replace( "/'/", "http://www.cnblogs.com/visoeclipse/admin/file://'/", $v );
//$v = preg_replace( "/#/", "\\#", $v );
$field_names .= "$k,";
$field_values .= "'$v',";
}

$field_names = preg_replace( "/,$/" , "" , $field_names );
$field_values = preg_replace( "/,$/" , "" , $field_values );

return array( 'FIELD_NAMES' => $field_names,
'FIELD_VALUES' => $field_values,
);
}

/*========================================================================*/
// Create an array from a multidimensional array returning a formatted
// string ready to use in an UPDATE query, saves having to manually format
// the FIELD='val', FIELD='val', FIELD='val'
/*========================================================================*/

function compile_db_update_string($data) {

$return_string = "";

foreach ($data as $k => $v)
{
$v = preg_replace( "/'/", "http://www.cnblogs.com/visoeclipse/admin/file://'/", $v );
$return_string .= $k . "='".$v."',";
}

$return_string = preg_replace( "/,$/" , "" , $return_string );

return $return_string;
}

function halt($msg){

global $technicalemail,$debug;

$message = "<html>\n<head>\n";
$message .= "<meta content=\"text/html; charset=gb2312\" http-equiv=\"Content-Type\">\n";
$message .= "<STYLE TYPE=\"text/css\">\n";
$message .= "<!--\n";
$message .= "body,td,p,pre {\n";
$message .= "font-family : Verdana, Arial, Helvetica, sans-serif;font-size : 12px;\n";
$message .= "}\n";
$message .= "</STYLE>\n";
$message .= "</head>\n";
$message .= "<body bgcolor=\"#EEEEEE\" text=\"#000000\" link=\"#006699\" vlink=\"#5493B4\">\n";
$message .= "<font size=10><b>系统调试</b></font><font size=6><b>(by 大白菜芯 )</b></font>\n<hr NOSHADE SIZE=1>\n";

$content = "<p>数据库出错:</p><pre><b>".htmlspecialchars($msg)."</b></pre>\n";
$content .= "<b>Mysql error description</b>: ".$this->geterrdesc()."\n<br>";
$content .= "<b>Mysql error number</b>: ".$this->geterrno()."\n<br>";
$content .= "<b>Date</b>: ".date("Y-m-d @ H:i")."\n<br>";
$content .= "<b>Script</b>: http://www.cnblogs.com/visoeclipse/admin/'http://>";
$content .= "<b>Referer</b>: ".getenv("HTTP_REFERER")."\n<br><br>";

$message .= $content;

$message .= "<p>请尝试刷新你的浏览器,如果仍然无法正常显示,请联系<a href=\"technicalemail."\'>mailto:".$this->technicalemail."\">管理员</a>.</p>";
$message .= "</body>\n</html>";
echo $message;

$headers = "From: nt.cn <$this->technicalemail>\r\n";

$content = strip_tags($content);
@mail($technicalemail,"数据库出错",$content,$headers);

exit;
}

}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: