php教程 mysql教程完整數據庫教程連接類
*/
class mysql {
private $db_host; //數據庫主機
private $db_user; //數據庫用戶名
private $db_pwd; //數據庫用戶名密碼
private $db_database; //數據庫名
private $conn; //數據庫連接標識;
private $result; //執行query命令的結果資源標識
private $sql; //sql執行語句
private $row; //返回的條目數
private $coding; //數據庫編碼,gbk,utf8,gb2312
private $bulletin = true; //是否開啟錯誤記錄
private $show_error = false; //測試階段,顯示所有錯誤,具有安全隱患,默認關閉
private $is_error = false; //發現錯誤是否立即終止,默認true,建議不啟用,因為當有問題時用戶什麼也看不到是很苦惱的/*構造函數*/
public function __construct($db_host, $db_user, $db_pwd, $db_database, $conn, $coding) {
$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);
}
}
mysql_query("set names $this->coding");
}/*數據庫執行語句,可執行查詢添加修改刪除等任何sql語句*/
public function query($sql) {
if ($sql == "") {
$this->show_error("sql語句錯誤:", "sql查詢語句為空");
}
$this->sql = $sql;$result = mysql_query($this->sql, $this->conn);
if (!$result) {
//調試中使用,sql語句出錯時會自動打印出來
if ($this->show_error) {
$this->show_error("錯誤sql語句:", $this->sql);
}
} else {
$this->result = $result;
}
return $this->result;
}/*創建添加新的數據庫*/
public function create_database($database_name) {
$database = $database_name;
$sqldatabase = 'create database ' . $database;
$this->query($sqldatabase);
}/*查詢服務器所有數據庫*/
//將系統數據庫與用戶數據庫分開,更直觀的顯示?
public function show_databases() {
$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++;
}
}/*
mysql_fetch_row() array $row[0],$row[1],$row[2]
mysql_fetch_array() array $row[0] 或 $row[id]
mysql_fetch_assoc() array 用$row->content 字段大小寫敏感
mysql_fetch_object() object 用$row[id],$row[content] 字段大小寫敏感
*//*取得結果數據*/
public function mysql_result_li() {
return mysql_result($str);
}/*取得記錄集,獲取數組-索引和關聯,使用$row['content'] */
public function fetch_array($resultt="") {
if($resultt<>""){
return mysql_fetch_array($resultt);
}else{
return mysql_fetch_array($this->result);
}
}//獲取關聯數組,使用$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 = '') {
//echo "update $table set $mod_content where $condition"; exit();
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 . "rn$this->sql" . "rn客戶ip:$ip" . "rn時間 :$time" . "rnrn";$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)) {
//默認的 mode 是 0777,意味著最大可能的訪問權
die("upload files directory does not exist and creation failed");
}
}//建立txt日期文件
if (!file_exists($file_path)) {//echo "建立日期文件";
fopen($file_path, "w+");//首先要確定文件存在並且可寫
if (is_writable($file_path)) {
//使用添加模式打開$filename,文件指針將會在文件的開頭
if (!$handle = fopen($file_path, 'a')) {
echo "不能打開文件 $filename";
exit;
}//將$somecontent寫入到我們打開的文件中。
if (!fwrite($handle, $error_content)) {
echo "不能寫入到文件 $filename";
exit;
}//echo "文件 $filename 寫入成功";
echo "——錯誤記錄被保存!";
//關閉文件
fclose($handle);
} else {
echo "文件 $filename 不可寫";
}} else {
//首先要確定文件存在並且可寫
if (is_writable($file_path)) {
//使用添加模式打開$filename,文件指針將會在文件的開頭
if (!$handle = fopen($file_path, 'a')) {
echo "不能打開文件 $filename";
exit;
}//將$somecontent寫入到我們打開的文件中。
if (!fwrite($handle, $error_content)) {
echo "不能寫入到文件 $filename";
exit;
}//echo "文件 $filename 寫入成功";
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) {
//return mysql_num_fields($this->result);
$this->query("select * from $table_name");
echo "<br />";
echo "字段數:" . $total = mysql_num_fields($this->result);
echo "<pre>";
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(); //mysql 服務器信息
break;case 2 :
return mysql_get_host_info(); //取得 mysql 主機信息
break;case 3 :
return mysql_get_client_info(); //取得 mysql 客戶端信息
break;case 4 :
return mysql_get_proto_info(); //取得 mysql 協議信息
break;default :
return mysql_get_client_info(); //默認取得mysql版本信息
}
}//析構函數,自動關閉數據庫,垃圾回收機制
public function __destruct() {
if (!empty ($this->result)) {
$this->free();
}
mysql_close($this->conn);
} //function __destruct();/*獲得客戶端真實的ip地址*/
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);
}
function inject_check($sql_str) { //防止注入
$check = eregi('select|insert|update|delete|'|/*|*|../|./|union|into|load_file|outfile', $sql_str);
if ($check) {
echo "輸入非法注入內容!";
exit ();
} else {
return $sql_str;
}
}
function checkurl() { //檢查來路
if (preg_replace("/https教程?://([^:/]+).*/i", "1", $_server['http_referer']) !== preg_replace("/([^:]+).*/", "1", $_server['http_host'])) {
header("location: http://www.zhutiai.com");
exit();
}
}}