/*這個基本上就是我使用的數據庫類*/
//屏蔽未定義錯誤
error_reporting(7);
class DB_MySQL {
var $servername="localhost";
var $dbname="DBASE";
var $dbusername = "ROOT";
var $dbpassword = "";
var $conn = 0;
var $technicalemail='java@cu165.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( "/'/", "\\'", $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( "/'/", "\\'", $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\" <$this->technicalemail>\r\n";
$content = strip_tags($content);
@mail($technicalemail,"數據庫出錯",$content,$headers);
exit;
}
}
>