<?php
/**
* 數據庫操作類
* 2011/8/25
* kcj
* */
class MyDB {
private $db_host; //數據庫主機名
private $db_user; //數據庫用戶名
private $db_pwd; //數據庫密碼
private $db_database; //數據庫名
private $conn; //連接標識
private $result; //執行query命令的結果資源標識
private $row; //返回的條目數
private $sql; //sql執行語句
private $coding; //數據庫編碼
private $bulletin=true; // 是否開啟錯誤記錄
private $show_error=false; //測試階段,顯示所有錯誤,具有安全隱患,默認關閉
private $is_error=false; //發現錯誤是否立即終止,默認true,建議不啟用,因為當有問題時用戶什麼也看不到是很苦惱的
//構造函數
function __construct($db_host,$db_user,$db_pwd,$db_database,$conn,$doding){
$this->db_host=$db_host;
$this->db_user=$db_user;
$this->db_pwd=$db_pwd;
$this->db_database=$db_database;
$this->conn=$conn;
$this->coding=$coding;
$this->connect();
}
//數據庫連接
public function connect(){
if($this->conn=="pconn"){
//永久連接
$this->conn=mysql_pconnect($this->db_host,$this->db_user,$this->db_pwd);
}else{
//即使連接
$this->conn=mysql_connect($this->db_host,$this->db_user,$this->db_pwd);
}
if(!mysql_select_db($this->db_database,$this->conn)){
if($this->show_error){
$this->show_error("數據庫不可用:",$this->db_database);
}
}
}
//數據庫執行語句,可執行查詢添加修改刪除等任何sql語句
public function query($sql){
if($sql==""){
$this->show_error("sql語句錯誤:","sql語句為空");
}
$this->sql=$sql;
$result=mysql_query($this->sql,$this->conn);
if(!$result){
if($this->show_error){
$this->show_error("錯誤sql語句:",$this->sql);
}
}else {
$this->result;
}
return $result;
}
//創建添加新的數據庫
public function create_database($database_name){
$database=$database_name;
$sqlDatabase='create database'.$database;
$this->query($sqlDatabase);
}
//查詢服務器所有數據庫
//將系統數據庫與用戶分開,更直觀的顯示
public function show_database(){
$this->query("show databases");
echo "現在有的數據庫:".$amount=$this->db_num_rows($rs);
echo "<br>";
$i=1;
while ($row=$this->fetch_array($rs)){
echo "$i $row[Database]";
echo "<br>";
$i++;
}
}
//以數組的形式返回主機中所有的數據庫名
public function databases(){
$rsPtr=mysql_list_dbs($this->conn);
$i=0;
$cnt=mysql_num_rows($rsPtr);
while ($i<$cnt){
$rs[]=mysql_db_name($rsPtr,$i);
$i++;
}
return $rs;
}
//查詢數據庫下所有的表
public function show_tables($database_name){
$this->query("show tables");
echo "現有數據庫:".$amount=$this->db_num_rows($rs);
echo "<br>";
$i=1;
while ($row=$this->fetch_array($rs)){
$columnName="Tables_in_".$database_name;
echo "$i $row[$columnName]";
echo "<br>";
$i++;
}
}
// 取得結果集
public function fetch_array($resultt=""){
if($resultt!=""){
return mysql_fetch_array($resultt);
}else {
return mysql_fetch_array($this->result);
}
}
//取得結果數 $row['content']
public function mysql_result_li(){
return mysql_result($str);
}
//獲取關聯數組 $row['字段名']
public function fetch_assoc(){
return mysql_fetch_assoc($this->result);
}
//獲取數字索引數組 $row[0] $row[1] $row[2]
public function fetch_row(){
return mysql_fetch_row($this->result);
}
//獲取對象數組,使用$row->content
public function fetch_Object(){
return mysql_fetch_object($this->result);
}
//簡化查詢select
public function findall($table){
$this->query("select* from $table");
}
//簡化查詢select
public function select($table,$columnName="*",$condition='',$debug=''){
$condition=$condition?'where'.$condition:null;
if($debug){
echo "select $columnName from $table $condition";
}else{
$this->query("select $columnName from $table $condition");
}
}
//簡化刪除del
public function delete($table,$condition,$url=''){
if($this->query("delete from $table where $condition")){
if(!emptyempty($url)){
$this->Get_admin_msg($url,'刪除成功');
}
}
}
//簡化插入insert
public function insert($table,$columnName,$value,$url=''){
if($this->query("insert into $table ($columnName) values ($value)")){
if(!emptyempty($url)){
$this->Get_admin_msg($url,'添加成功');
}
}
}
//簡化更新update
public function update($table,$mod_content,$condition,$url=''){
if($this->query("update $table set $mod_content where $condition")){
if(!emptyempty($url)){
$this->Get_admin_msg($url);
}
}
}
//取得上一步insert操作的id
public function insert_id(){
return mysql_insert_id();
}
//指向確定的一條數據記錄
public function db_data_seek($id){
if($id>0){
$id=$id-1;
}
if(!@mysql_data_seek($this->result,$id)){
$this->show_error("sql語句有誤:","指定的數據為空");
}
return $this->result;
}
//根據select查詢結果計算結果集條數
public function db_num_rows(){
if($this->result=null){
if($this->show_error){
$this->show_error("sql語句錯誤:","暫時為空,沒有任何內容");
}
}else{
return mysql_num_rows($this->result);
}
}
//根據insert update delete執行的結果驅動影響行數
public function db_affected_rows(){
return mysql_affected_rows();
}
//輸出顯示sql語句
public function show_error($message="",$sql=""){
if(!$sql){
echo "<font color='red'>" . $message . "</font>";
echo "<br>";
}else{
echo "<fieldset>";
echo "<legend>錯誤信息提示:</legend><br />";
echo "<div style='font-size:14px; clear:both; font-family:Verdana, Arial, Helvetica, sans-serif;'>";
echo "<div style='height:20px; background:#000000; border:1px #000000 solid'>";
echo "<font color='white'>錯誤號:12142</font>";
echo "</div><br />";
echo "錯誤原因:" . mysql_error() . "<br /><br />";
echo "<div style='height:20px; background:#FF0000; border:1px #FF0000 solid'>";
echo "<font color='white'>" . $message . "</font>";
echo "</div>";
echo "<font color='red'><pre>" . $sql . "</pre></font>";
$ip = $this->getip();
if ($this->bulletin) {
$time = date("Y-m-d H:i:s");
$message = $message . "\r\n$this->sql" . "\r\n客戶IP:$ip" . "\r\n時間 :$time" . "\r\n\r\n";
$server_date = date("Y-m-d");
$filename = $server_date . ".txt";
$file_path = "error/" . $filename;
$error_content = $message;
//$error_content="錯誤的數據庫,不可以鏈接";
$file = "error"; //設置文件保存目錄
//建立文件夾
if(!file_exists($file)){
if(!mkdir($file,0777)){
die("upload files directory does not exist and creation failed");
}
}
//建立txt日期文件
if(!file_exists($file_path)){
fopen($file_path,"w+");
if(is_writable($file_path)){
if(!$handle=fopen($file_path,'a')){
echo "不能打開文件 $filename";
exit;
}
if(!fwrite($handle,$error_content)){
echo "不能寫到文件 $filename";
exit;
}
echo "——錯誤記錄被保存!";
fclose($handle);
}else {
echo "文件 $filename 不可寫";
}
}else {
if(is_writable($file_path)){
if(!$handle=fopen($file_path,'a')){
echo "不能打開文件 $filename";
exit;
}
if(!fwrite($handle,$error_content)){
echo "不能寫入文件 $filename";
exit;
}
echo "——錯誤記錄被保存!";
fclose($handle);
}else {
echo "文件 $filename 不可寫";
}
}
}
echo "<br />";
if ($this->is_error) {
exit;
}
}
echo "</div>";
echo "</fieldset>";
echo "<br/>";
}
//釋放結果集
public function free(){
@mysql_free_result($this->result);
}
//數據庫選擇
public function select_db($db_database){
return mysql_select_db($db_database);
}
//查詢字段數量
public function num_fields($table_name){
$this->query("select * from $table_name");
echo "<br>";
echo "字段數:".$total=mysql_num_fields($this->result);
for ($i=0;$i<$total;$i++){
print_r(mysql_fetch_field($this->result,$i));
}
echo "</pre>";
echo "<br>";
}
//取得mysql 服務器信息
public function mysql_server($num=''){
switch ($num){
case 1:
return mysql_get_server_info();
break;
case 2:
return mysql_get_host_info();
break;
case 3:
return mysql_get_client_info();
break;
case 4:
return mysql_get_proto_info();
break;
default:
return mysql_get_client_info();
}
}
public function __destruct(){
if(!emptyempty($this->result)){
$this->free();
}
mysql_close($this->conn);
}
//獲得客戶端真實的ID地址
function getip() {
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
$ip = getenv("HTTP_CLIENT_IP");
} else
if (getenv("HTTP_X_FORWARDED_FOR") &&strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
$ip = getenv("HTTP_X_FORWARDED_FOR");
} else
if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
$ip = getenv("REMOTE_ADDR");
} else
if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] &&strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
$ip = $_SERVER['REMOTE_ADDR'];
} else {
$ip = "unknown";
}
return ($ip);
}
}
?>
<?php
/**
* 數據庫操作類
* 2011/8/25
* kcj
* */
class MyDB {
private $db_host; //數據庫主機名
private $db_user; //數據庫用戶名
private $db_pwd; //數據庫密碼
private $db_database; //數據庫名
private $conn; //連接標識
private $result; //執行query命令的結果資源標識
private $row; //返回的條目數
private $sql; //sql執行語句
private $coding; //數據庫編碼
private $bulletin=true; // 是否開啟錯誤記錄
private $show_error=false; //測試階段,顯示所有錯誤,具有安全隱患,默認關閉
private $is_error=false; //發現錯誤是否立即終止,默認true,建議不啟用,因為當有問題時用戶什麼也看不到是很苦惱的
//構造函數
function __construct($db_host,$db_user,$db_pwd,$db_database,$conn,$doding){
$this->db_host=$db_host;
$this->db_user=$db_user;
$this->db_pwd=$db_pwd;
$this->db_database=$db_database;
$this->conn=$conn;
$this->coding=$coding;
$this->connect();
}
//數據庫連接
public function connect(){
if($this->conn=="pconn"){
//永久連接
$this->conn=mysql_pconnect($this->db_host,$this->db_user,$this->db_pwd);
}else{
//即使連接
$this->conn=mysql_connect($this->db_host,$this->db_user,$this->db_pwd);
}
if(!mysql_select_db($this->db_database,$this->conn)){
if($this->show_error){
$this->show_error("數據庫不可用:",$this->db_database);
}
}
}
//數據庫執行語句,可執行查詢添加修改刪除等任何sql語句
public function query($sql){
if($sql==""){
$this->show_error("sql語句錯誤:","sql語句為空");
}
$this->sql=$sql;
$result=mysql_query($this->sql,$this->conn);
if(!$result){
if($this->show_error){
$this->show_error("錯誤sql語句:",$this->sql);
}
}else {
$this->result;
}
return $result;
}
//創建添加新的數據庫
public function create_database($database_name){
$database=$database_name;
$sqlDatabase='create database'.$database;
$this->query($sqlDatabase);
}
//查詢服務器所有數據庫
//將系統數據庫與用戶分開,更直觀的顯示
public function show_database(){
$this->query("show databases");
echo "現在有的數據庫:".$amount=$this->db_num_rows($rs);
echo "<br>";
$i=1;
while ($row=$this->fetch_array($rs)){
echo "$i $row[Database]";
echo "<br>";
$i++;
}
}
//以數組的形式返回主機中所有的數據庫名
public function databases(){
$rsPtr=mysql_list_dbs($this->conn);
$i=0;
$cnt=mysql_num_rows($rsPtr);
while ($i<$cnt){
$rs[]=mysql_db_name($rsPtr,$i);
$i++;
}
return $rs;
}
//查詢數據庫下所有的表
public function show_tables($database_name){
$this->query("show tables");
echo "現有數據庫:".$amount=$this->db_num_rows($rs);
echo "<br>";
$i=1;
while ($row=$this->fetch_array($rs)){
$columnName="Tables_in_".$database_name;
echo "$i $row[$columnName]";
echo "<br>";
$i++;
}
}
// 取得結果集
public function fetch_array($resultt=""){
if($resultt!=""){
return mysql_fetch_array($resultt);
}else {
return mysql_fetch_array($this->result);
}
}
//取得結果數 $row['content']
public function mysql_result_li(){
return mysql_result($str);
}
//獲取關聯數組 $row['字段名']
public function fetch_assoc(){
return mysql_fetch_assoc($this->result);
}
//獲取數字索引數組 $row[0] $row[1] $row[2]
public function fetch_row(){
return mysql_fetch_row($this->result);
}
//獲取對象數組,使用$row->content
public function fetch_Object(){
return mysql_fetch_object($this->result);
}
//簡化查詢select
public function findall($table){
$this->query("select* from $table");
}
//簡化查詢select
public function select($table,$columnName="*",$condition='',$debug=''){
$condition=$condition?'where'.$condition:null;
if($debug){
echo "select $columnName from $table $condition";
}else{
$this->query("select $columnName from $table $condition");
}
}
//簡化刪除del
public function delete($table,$condition,$url=''){
if($this->query("delete from $table where $condition")){
if(!empty($url)){
$this->Get_admin_msg($url,'刪除成功');
}
}
}
//簡化插入insert
public function insert($table,$columnName,$value,$url=''){
if($this->query("insert into $table ($columnName) values ($value)")){
if(!empty($url)){
$this->Get_admin_msg($url,'添加成功');
}
}
}
//簡化更新update
public function update($table,$mod_content,$condition,$url=''){
if($this->query("update $table set $mod_content where $condition")){
if(!empty($url)){
$this->Get_admin_msg($url);
}
}
}
//取得上一步insert操作的id
public function insert_id(){
return mysql_insert_id();
}
//指向確定的一條數據記錄
public function db_data_seek($id){
if($id>0){
$id=$id-1;
}
if(!@mysql_data_seek($this->result,$id)){
$this->show_error("sql語句有誤:","指定的數據為空");
}
return $this->result;
}
//根據select查詢結果計算結果集條數
public function db_num_rows(){
if($this->result=null){
if($this->show_error){
$this->show_error("sql語句錯誤:","暫時為空,沒有任何內容");
}
}else{
return mysql_num_rows($this->result);
}
}
//根據insert update delete執行的結果驅動影響行數
public function db_affected_rows(){
return mysql_affected_rows();
}
//輸出顯示sql語句
public function show_error($message="",$sql=""){
if(!$sql){
echo "<font color='red'>" . $message . "</font>";
echo "<br>";
}else{
echo "<fieldset>";
echo "<legend>錯誤信息提示:</legend><br />";
echo "<div style='font-size:14px; clear:both; font-family:Verdana, Arial, Helvetica, sans-serif;'>";
echo "<div style='height:20px; background:#000000; border:1px #000000 solid'>";
echo "<font color='white'>錯誤號:12142</font>";
echo "</div><br />";
echo "錯誤原因:" . mysql_error() . "<br /><br />";
echo "<div style='height:20px; background:#FF0000; border:1px #FF0000 solid'>";
echo "<font color='white'>" . $message . "</font>";
echo "</div>";
echo "<font color='red'><pre>" . $sql . "</pre></font>";
$ip = $this->getip();
if ($this->bulletin) {
$time = date("Y-m-d H:i:s");
$message = $message . "\r\n$this->sql" . "\r\n客戶IP:$ip" . "\r\n時間 :$time" . "\r\n\r\n";
$server_date = date("Y-m-d");
$filename = $server_date . ".txt";
$file_path = "error/" . $filename;
$error_content = $message;
//$error_content="錯誤的數據庫,不可以鏈接";
$file = "error"; //設置文件保存目錄
//建立文件夾
if(!file_exists($file)){
if(!mkdir($file,0777)){
die("upload files directory does not exist and creation failed");
}
}
//建立txt日期文件 www.2cto.com
if(!file_exists($file_path)){
fopen($file_path,"w+");
if(is_writable($file_path)){
if(!$handle=fopen($file_path,'a')){
echo "不能打開文件 $filename";
exit;
}
if(!fwrite($handle,$error_content)){
echo "不能寫到文件 $filename";
exit;
}
echo "——錯誤記錄被保存!";
fclose($handle);
}else {
echo "文件 $filename 不可寫";
}
}else {
if(is_writable($file_path)){
if(!$handle=fopen($file_path,'a')){
echo "不能打開文件 $filename";
exit;
}
if(!fwrite($handle,$error_content)){
echo "不能寫入文件 $filename";
exit;
}
echo "——錯誤記錄被保存!";
fclose($handle);
}else {
echo "文件 $filename 不可寫";
}
}
}
echo "<br />";
if ($this->is_error) {
exit;
}
}
echo "</div>";
echo "</fieldset>";
echo "<br/>";
}
//釋放結果集
public function free(){
@mysql_free_result($this->result);
}
//數據庫選擇
public function select_db($db_database){
return mysql_select_db($db_database);
}
//查詢字段數量
public function num_fields($table_name){
$this->query("select * from $table_name");
echo "<br>";
echo "字段數:".$total=mysql_num_fields($this->result);
for ($i=0;$i<$total;$i++){
print_r(mysql_fetch_field($this->result,$i));
}
echo "</pre>";
echo "<br>";
}
//取得mysql 服務器信息
public function mysql_server($num=''){
switch ($num){
case 1:
return mysql_get_server_info();
break;
case 2:
return mysql_get_host_info();
break;
case 3:
return mysql_get_client_info();
break;
case 4:
return mysql_get_proto_info();
break;
default:
return mysql_get_client_info();
}
}
public function __destruct(){
if(!empty($this->result)){
$this->free();
}
mysql_close($this->conn);
}
//獲得客戶端真實的ID地址
function getip() {
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
$ip = getenv("HTTP_CLIENT_IP");
} else
if (getenv("HTTP_X_FORWARDED_FOR") &&strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
$ip = getenv("HTTP_X_FORWARDED_FOR");
} else
if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
$ip = getenv("REMOTE_ADDR");
} else
if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] &&strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
$ip = $_SERVER['REMOTE_ADDR'];
} else {
$ip = "unknown";
}
return ($ip);
}
}
?>
摘自 chaojie2009的專欄