具体代码解释.
一: MYSQL.PHP
---------------------------MYSQL.PHP原代码开始-----------------------------
<?php
//屏蔽未定义错误
error_reporting(7);class DB_MySQL { var $servername="localhost";
var $dbname="love";
var $dbusername = "root";
var $dbpassword = "";
var $conn = 0;
var $technicalemail='[email protected]';
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;
}
一: MYSQL.PHP
---------------------------MYSQL.PHP原代码开始-----------------------------
<?php
//屏蔽未定义错误
error_reporting(7);class DB_MySQL { var $servername="localhost";
var $dbname="love";
var $dbusername = "root";
var $dbpassword = "";
var $conn = 0;
var $technicalemail='[email protected]';
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') /* 例如: $str = $DB->compile_db_insert_string( array (
'id' => $unique_id,
'search_date'=> time(),
'post_id' => $posts,
'post_max' => $max_hits,
'sort_key' => 'p.post_date',
'sort_order' => 'desc',
'member_id' => $ibforums->member['id'],
'ip_address' => $ibforums->input['IP_ADDRESS'],
) );
$DB->query("INSERT INTO ibf_search_results ({$str['FIELD_NAMES']}) VALUES ({$str['FIELD_VALUES']})");
*/
/*========================================================================*/
function compile_db_insert_string($data) {
$field_names = "";
$field_values = "";
foreach ($data as $k => $v)
{
$v = preg_replace( "/'/", "\\'", $v );
$v = preg_replace( "/script/", "\\common", $v );
$v = preg_replace( "/SCRIPT/", "\\common", $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' /例如: $db_string = $DB->compile_db_update_string( array (
'last_post' => $last['post_date'],
'last_poster_id' => $last['author_id'],
'last_poster_name' => $last['author_name'],
) );
$DB->query("UPDATE ibf_topics SET $db_string WHERE tid='".$ibforums->input['tid']."'");
*/
/*========================================================================*/
function compile_db_update_string($data) {
$return_string = "";
foreach ($data as $k => $v)
{
$v = preg_replace( "/'/", "\\'", $v );
$v = preg_replace( "/script/", "\\common", $v );
$v = preg_replace( "/SCRIPT/", "\\common", $v );
$return_string .= $k . "='".$v."',";
}
$return_string = preg_replace( "/,$/" , "" , $return_string );
return $return_string;
} function halt($msg){
echo $msg;
exit;
}
/*
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://".$_SERVER[HTTP_HOST].getenv("REQUEST_URI")."\n<br>";
$content .= "<b>Referer</b>: ".getenv("HTTP_REFERER")."\n<br><br>";
$message .= $content;
$message .= "<p>请尝试刷新你的浏览器,如果仍然无法正常显示,请联系<a href=\"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;
}*/
}
?>
---------------------------MYSQL.PHP原代码结束-----------------------------
以上是我使用的MYSQL.PHP 代码.主要用来对数据库做操作的.