- /*
- * Author Molong
- * Time December 2, 2010 15:50:35
- */
- $db = new mysql($db_host,$db_user,$db_password, $db_table,$db_conn,$pre,$coding);
- class mysql{
- private $db_host;
- private $db_user;
- private $db_password;
- private $db_table;
- private $db_conn; //Database connection identification;
- private $result; //Result resource identification of executing query command
- private $sql; //SQL execution statement
- private $pre; //Database table prefix
- private $coding; //Database encoding, 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("Database link error, please check the database link configuration! "));
- if(!mysql_select_db($this->db_table,$this->db_conn)){
-
- echo "Data table not found: ".$this->db_table;
- }
- mysql_select_db($ this->db_table,$this->db_conn);
- $this->query("SET NAMES $this->coding");
- }
-
- /*Function to execute SQL statements*/
- function query ($sql){
-
- if(emptyempty($sql)){
- $this->show_error("Your sql statement cannot be empty!");
- }else{
- $this->sql = $sql;
- }
- $result = mysql_query($this->sql,$this->db_conn);
-
- return $this->result = $result;
- }
-
- /*Create and add a new database*/
- public function create_database($database_name){
- $database=$database_name;
- $sqlDatabase = 'create database '.$database;
- return $this- >query($sqlDatabase);
- }
-
- // Calculate the number of result set items based on the select query results
- public function db_num_rows(){
- if($this->result==null){
- if($this- >show_error){
- $this->show_error("sql statement error!");
- }
- }else{
- return mysql_num_rows($this->result);
- }
- }
-
- /*Query all server Database*/
- //Separate the system database and the user database for a more intuitive display?
- public function show_databases(){
- $this->query("show databases");
- echo "Existing database:".$ amount =$this->db_num_rows($rs);
- echo "
";
- $i=1;
- while($row = $this->fetch_array($rs)){
- echo "$i $row[Database]";
- echo "
";
- $i++;
- }
- }
-
- //Return all database names in the host in array form
- public function databases()
- {
- $rsPtr=mysql_list_dbs($this->db_conn);
- $i=0;
- $cnt=mysql_num_rows($rsPtr);
- while($i<$cnt)
- {
- $rs[]=mysql_db_name($ rsPtr,$i);
- $i++;
- }
- return print_r($rs);
- }
- /*Query all tables under the database*/
- function show_tables($database_name){
- $this->query( "show tables");
- echo "Existing database:".$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] or $row[id]
- mysql_fetch_assoc() array uses $row->content field case sensitivity
- mysql_fetch_object() object uses $row[id],$row[content] fields case sensitivity
- */
- /*Get the record set, get the array-index and association, use $row['content'] */
- public function fetch_array()
- {
- return @mysql_fetch_array($this->result);
- }
-
- //Get the association array, use $row[' field name']
- public function fetch_ass()
- {
- return @mysql_fetch_assoc($this->result);
- }
-
- //Get the numeric index array, use $row[0],$row[1],$row [2]
- public function fetch_row()
- {
- return @mysql_fetch_row($this->result);
- }
-
- //Get the object array, use $row->content
- public function fetch_Object()
- {
- return @mysql_fetch_object($this->result);
- }
-
- //Simplified query select
- public function findall($table){
- $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");
- }
-
- //Simplified insert
- function insert($table ,$arr){
- $table = $this->fulltablename($table);
- $sql = "INSERT INTO $table ";
- if(!is_array($arr)){
- $this->show_error( "Please enter the parameter array!");
- }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);
- }
- //简化的update
- 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);
- }
- //Simplified delete
- function delete($ table,$where = ""){
- $table = $this->fulltablename($table);
- if(emptyempty($where)){
- $this->show_error("Condition cannot be empty!") ;
- }else{
- $where = " where ".$where;
- }
- $sql = "DELETE FROM $table ".$where;
- //echo $sql;
- return $this->query($sql );
- }
-
- //Get the ID generated by the previous INSERT operation
- public function insert_id(){
- return mysql_insert_id();
- }
-
- //Prefixed data table
- public function fulltablename($table){
- return $table = $this->pre.$table;
- }
-
- //Query the number of fields
- public function num_fields($table){
- $table = $this->fulltablename($table);
- $ this->query("select * from $table");
- echo "
";
- echo "Number of fields: ".$total = mysql_num_fields($this->result);
- echo "
";
- for ($i=0; $i<$total; $i++){
- print_r(mysql_fetch_field($this->result,$i) );
- }
- echo "
- echo "
";
- }
-
- //Get MySQL server information
- public function mysql_server($num=''){
- switch ($num){
- case 1:
- return mysql_get_server_info (); //MySQL server information
- break;
-
- case 2 :
- return mysql_get_host_info(); //Get MySQL host information
- break;
-
- case 3 :
- return mysql_get_client_info(); //Get MySQL client information
- break;
-
- case 4:
- return mysql_get_proto_info(); //Get MySQL protocol information
- break;
-
- default:
- return mysql_get_client_info(); //Get mysql version information by default
- }
- }
-
- //Destructor , automatically close the database, garbage collection mechanism
- /*public function __destruct()
- {
- if(!empty($this->result)){
- $this->free();
- }
- mysql_close($this ->$db_conn);
- }*/
-
- /*Get the real IP address of the client*/
- 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 show_error($str){
- echo "";
- }
-
- }
- ?>
Copy code
|