- /*
- * 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; The result resource ID of the Execute Query command
- Private $sql; SQL execution Statements
- Private $pre; database table prefixes
- 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 database link configuration!") "));
- if (!mysql_select_db ($this->db_table, $this->db_conn)) {
- echo "No data table found:". $this->db_table;
- }
- mysql_select_db ($this->db_table, $this->db_conn);
- $this->query ("SET NAMES $this->coding");
- }
- /* function to execute SQL statement */
- 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 result set bars based on 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 server for all databases */
- Separate the system database from 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 + +;
- }
- }
- Returns all database names in the host as an array
- 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 is case sensitive with $row->content fields
- Mysql_fetch_object () object with $row[id], $row [content] field is case sensitive
- */
- /* Get Recordset, get array-Index and association, use $row[' content ' */
- Public Function Fetch_array ()
- {
- Return @mysql_fetch_array ($this->result);
- }
- Get associative array, use $row[' field name ']
- Public Function Fetch_ass ()
- {
- Return @mysql_fetch_assoc ($this->result);
- }
- Get a numeric index array, using $row[0], $row [1], $row [2]
- Public Function Fetch_row ()
- {
- Return @mysql_fetch_row ($this->result);
- }
- Get an array of objects, using $row->content
- Public Function Fetch_object ()
- {
- Return @mysql_fetch_object ($this->result);
- }
- Simplify 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);
- }
- Simplified update
- function Update ($table, $arr, $where) {
- $table = $this->fulltablename ($table);
- $sql = "UPDATE $table SET";
- if (!is_array ($arr)) {
- $this->show_error ("Please enter the parameter array! ");
- }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 ("Conditions cannot be empty!");
- }else{
- $where = "where". $where;
- }
- $sql = "DELETE from $table". $where;
- Echo $sql;
- return $this->query ($sql);
- }
- Gets the ID generated by the INSERT operation in the previous step
- Public Function insert_id () {
- return mysql_insert_id ();
- }
- Data table with prefix
- Public Function Fulltablename ($table) {
- return $table = $this->pre. $table;
- }
- Number of query 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 (); MySQL version information is obtained by default
- }
- }
- destructor, automatically shuts down the database, garbage collection mechanism
- /*public function __destruct ()
- {
- if (!empty ($this->result)) {
- $this->free ();
- }
- Mysql_close ($this, $db _conn);
- }*/
- /* Get the client's real IP address */
- 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
|