-
/*
- * Author 墨龍
- * Time 2010年12月2日15:50:35
- * Time 2010年2月2日15:50:35
- */
- */
-
- $db = new mysql($db_host,$db_user,$db_password,$db_table,$db_conn,$pre,$coding);
-
- class mysql{
-
- private $host ;
- private $db_user;
- private $db_password;
- private $db_table;
- private $db_conn; //資料庫連線識別碼;
- private $result;識別
- private $sql; //sql執行語句
- private $pre; //資料庫表前綴
- private $coding; //資料庫編碼,GBK,UTF8,gb2312
-
- function __construct ($db_host,$db_user,$db_password,$db_table,$db_conn,$pre,$coding){
-
- $this->db_host = $db_host;
- $this->db_user = $db_user;
- $this->db_password = $db_password;
- $this->db_table = $db_table;
- $this->db_conn = $db_conn;
- $this->pre = $pre;
- $this->coding = $coding;
- $this->connect();
-
- }
-
- function connect(){
-
- $this->db_conn = @mysql_connect($this->db_host,$this->db_user,$this->db_password) or die($this->show_error("資料庫連結錯誤,請檢查資料庫連結設定! "));
- if(!mysql_select_db($this->db_table,$this->db_conn)){
-
- echo "找不到資料表:".$this->db_table;
- }
- mysql_select_db($this->db_table,$this->db_conn);
- $this->query("SET NAMES $this->coding");
- }
-
- / *執行SQL語句的函數*/
- function query($sql){
- if(emptyempty($sql)){ $this->show_error("你的sql語句不能為空!");
- }else{
- $this->sql = $sql;
- }
- $result = mysql_query($this->sql,$this->db_conn);
-
- return $this->result = $result;
- }
-
- /*建立新增新的資料庫*/
- public function create_database($databasename){ $database=$base_name ;
- $sqlDatabase = 'create database '.$database;
- return $this->query($sqlDatabase);
- }
-
- // 根據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);
- }
- }
-
- /*查詢服務器所有數據庫*/
- //將系統資料庫與使用者資料庫分開,更直覺的顯示? .$amount =$this->db_num_rows($rs);
- echo "
";
- $i=1;
- while($row = $this->fetch_array($rs) ){
- echo "$i $row[Database]";
- echo "
";
- $i ;
- }
- }
-
- //以陣列形式傳回主機中所有資料庫名稱
- public function databases()
- {
- $rsPtr=mysql_list_dbs($this->db_conn);
- $i=0;
- $cnt=mysql_rows ($rsPtr);
- while($i {
- $rs[]=mysql_db_name($rsPtr,$i);
- $i ;
- }
- }
- return print_r($rs);
- }
-
- /*查詢資料庫下所有的表格*/
- function show_tables($database_name){
- $this->query("show tables" );
- echo "現有資料庫:".$amount = $this->db_num_rows($rs);
- echo "
";
- $i=1;
- while( $row = $this->fetch_array($rs)){
- $columnName="Tables_in_".$database_name;
- echo "$i $row[$columnName]";
- echo "
";
- $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 用$rowid] [content] 欄位大小寫敏感
- */
- /*取得記錄集,取得陣列-索引與關聯,使用$row['content'] */
- public function fetch_array()
- {
- return @mysql_fetch_array($this->result);
- }
-
- //取得關聯陣列,使用$row['欄位名稱']
- public function fetch_ass()
- {
- 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);
- }
-
- //簡化查詢
- $table = $this->fulltablename($table);
- $this->query("select * from $table");
- }
-
- public function select($table ,$columnName,$condition){
- $table = $this->fulltablename($table);
- if(emptyempty($columnName)){
- $columnName = "*";
- } }
- $this->query("SELECT $columnName FROM $table $condition");
- }
-
- //簡化的insert
- function insert($table,$arr){
- $table = $this->fulltablename($table);
- $sql = "INSERT INTO $table ";
- if(!is_array($arr)){
- $this->show_error( "請輸入參數數組!");
- }else{
- $k = "";
- $v = "";
- foreach($arr as $key => $value){
- $k . = "`$key`,";
- $v .= "'".$value."',";
- }
- }
- $sql = $sql." (".substr($k,0,-1).") VALUES (".substr($v,0,-1).")";
- $this->query($sql);
- }
- // 簡化更新
- function update($table,$arr,$where){
- $table = $this->fulltablename($table)
- $sql = "UPDATE $table SET ";
- if(!is_array($arr)){
- $this->show_error("請輸入備份參數!");
- }else{
- foreach($arr as $key => $value){
- $sql .= " `".$key."` = '".$value." ' ,";
- }
- }
- $sql = substr($sql,0,-1)." where ".$where;
- return $this->query($sql) ;
- }
- //簡化刪除
- function delete($table,$where = ""){
- $table = $this->fullttablename($table); emptyempty($where)){
- $this->show_error("條件不可能為空!"
- }else{
- $where = " where ".$where
- } $sql = "DELETE FROM $table ".$where
- //echo $sql;
- return $this->query($sql); }
-
- //取得第一步INSERT操作生產ID
- public function insert_id (){
- return mysql_insert_id()
- }
-
- //附加編號調整
- public function fulltablename($table){
- return $table = $this->pre.$ table
- }
-
- //列數
- public function num_fields($table){
- $table = $ this->fulltablename($table);
- $this->query ("select * from $table");
- echo "
";
- echo "列數:".$total = mysql_num_fields($this->result);
- echo "
- for ($i=0; $i print_r(mysql_fetch_field($this->結果,$i) );
- }
- echo " "
- echo "
"; 🎜>
- //取得MySQL伺服器資訊
- public function mysql_server($num=''){
- switch ($num){
- case 1 :
- return mysql_get_server_info (); // MySQL 主機資訊
- break;
-
- case 2 :
- return mysql_get_host_info();
- case 3 :
- return mysql_get_client_info (); //取得MySQL客戶端資訊
-
-
- case 4 :
- return mysql_get_proto_info();
-
- 預設:
- return mysql_get_client_info(); //取得mysql版本資訊
- }
- }
-
- //分析函數、自動控制號設定、容器恢復機制
- /*public function __destruct( )
- {
- if(!empty($this->result)){
- $this->free();
- }
- mysql_close( $ this->$db_conn );
- }*/
-
- /*取得客戶的真實IP 位置*/
- function getip(){
- if(getenv("HTTP_CLIENT_IP" ) && strcasecmp (getenv("HTTP_CLIENT_IP) "), "未知"))
- {
- $ip = getenv("HTTP_CLIENT_IP");
- }
- else if (getenv("HTTP_X🎜> }
- else if (getenv("HTTP_X_FORWARD_FORca(c getenv("HTTP_X_FORWARDED_FOR") , "未知")){
- $ip = getenv("HTTP_X_FORWARDED_FOR");
- }
- else if (getenv("REMO_DDR" ), "未知") )
- {
- $ip = getenv("REMOTE_ADDR")
- }
- else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER[ 'REMOTE_ADDR' ] && strcasecmp($ _SERVER['REMOTE_ADDR'], "未知")){
- $ip = $_SERVER['REMOTE_ADDR'];
- }
- else{
- $ip = "未知"
- }
- return($ip);
- }
-
- function show_error($str){
- echo "alert ('".$str ." ');history.back(-1);";
- }
-
- }
- ?>
-
-
- 回傳
-
-
-
-
PHP
|