- /*
- * Filename:mysql Database Connection Class
- * Edit bbs.it-home.org
- */
- Class mysql{
- Private $db _host; Database Host
- Private $db _user; Database user Name
- Private $db _pwd; Database User name password
- Private $db _database; Database name
- Private $conn; database connection identification;
- Private $result; The result resource ID of the Execute Query command
- Private $sql; SQL execution Statements
- Private $row; Number of entries returned
- Private $coding; Database encoding, gbk,utf8,gb2312
- Private $bulletin = true; Whether to turn on error logging
- Private $show _error = true; Test phase, show all errors, have security implications, default off
- Private $is _error = false; If the error is immediately terminated, the default is true and the recommendation is not enabled because it is very distressing for the user to see nothing when there is a problem
- /* Constructor function */
- 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 ();
- }
- /* Database connection */
- Public Function Connect ()
- {
- if ($this->conn== "Pconn") {
- Permanent links
- $this->conn=mysql_pconnect ($this->db_host, $this->db_user, $this->db_pwd);
- }else{
- Instant links
- $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 ("Database unavailable:", $this->db_database);
- }
- }
- mysql_query ("SET NAMES $this->coding");
- }
- /* Database execution statement, executable query add modify delete and so on any SQL statement * *
- Public Function query ($sql)
- {
- if ($sql = = "") {
- $this->show_error ("SQL statement error:", "SQL query statement is empty");}
- $this->sql = $sql;
- $result = mysql_query ($this->sql, $this->conn);
- if (! $result) {
- Used in debugging, the SQL statement is automatically printed when an error occurs
- if ($this->show_error) {
- $this->show_error ("Error SQL statement:", $this->sql);
- }
- }else{
- $this->result = $result;
- }
- return $this->result;
- }
- /* Create and add a new database */
- Public Function Create_database ($database _name) {
- $database = $database _name;
- $sqlDatabase = ' Create database '. $database;
- $this->query ($sqlDatabase);
- }
- /* Query server for all databases */
- Separate the system database from the user database for a more intuitive display?
- Public Function show_databases () {
- $rs = $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->conn);
- $i = 0;
- $cnt =mysql_num_rows ($RSPTR);
- while ($i < $cnt)
- {
- $rs []=mysql_db_name ($rsPtr, $i);
- $i + +;
- }
- return $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 Results data */
- Public Function Mysql_result_li ()
- {
- Return mysql_result ($STR);
- }
- /* 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_assoc ()
- {
- 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)
- {
- $this->query ("SELECT * from $table");
- }
- Simplify Query Select
- Public Function Select ($table, $columnName, $condition)
- {
- if ($columnName = = "") {
- $columnName = "*";
- }
- $this->query ("Select $columnName from $table $condition");
- }
- Simplified Delete del
- Public Function Delete ($table, $condition) {
- $this->query ("DELETE from $table WHERE $condition");
- }
- Simplify inserting insert
- Public Function Insert ($table, $columnName, $value) {
- $this->query ("INSERT into $table ($columnName) VALUES ($value)");
- }
- Simplified Modify Update
- Public Function Update ($table, $mod _content, $condition) {
- $this->query ("UPDATE $table SET $mod _content WHERE $condition");
- }
- /* Get the id*/from the previous INSERT operation
- Public Function insert_id () {
- return mysql_insert_id ();
- }
- Point to a data record that is determined
- Public Function Db_data_seek ($id) {
- if ($id >0) {
- $id = $id-1;
- }
- if (! @mysql_data_seek ($this->result, $id)) {
- $this->show_error ("SQL statement error:", "specified data is empty");
- }
- return $this->result;
- }
- 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", "temporarily empty, no content!") ");
- }
- }else{
- Return mysql_num_rows ($this->result);
- }
- }
- Number of affected rows based on Insert,update,delete execution results
- Public Function db_affected_rows () {
- return Mysql_affected_rows ();
- }
- Output Display SQL statement
- Public Function show_error ($message = "", $sql = "") {
- if (! $sql) {
- echo "". $message. "";
- echo "
";
- }else{
- echo " ";
- echo " error message Tip:
";
- echo "Helvetica, Sans-serif;" > ";
- echo "";
- echo "Error number: 12142";
- echo "
";
- echo "Error Reason:". Mysql_error (). "
";
- echo "";
- echo "". $message. "";
- echo "";
- echo "
". $sql." ";
- $ip = $this->getip ();
- if ($this->bulletin) {
- $time = Date ("y-m-d h:i:s");
- $message = $message. " /r/n$this->sql "." /r/n Client IP: $ip "." /r/n time: $time "." /r/n/r/n ";
- $server _date=date ("y-m-d");
- $filename = $server _date. ". TXT ";
- $file _path= "error/". $filename;
- $error _content= $message;
- $error _content= "Wrong database, can not link";
- $file = "Error"; Settings File Save Directory
- Create a folder
- if (!file_exists ($file)) {
- if (!mkdir ($file, 0777)) {
- The default mode is 0777, which means the maximum possible access rights
- Die ("Upload files directory does not exist and creation failed");
- }
- }
- Create a txt date file
- if (!file_exists ($file _path)) {
- echo "Build date File";
- fopen ($file _path, "w+");
- The first thing to determine is that the file exists and is writable
- if (is_writable ($file _path))
- {
- Using Add mode to open $filename, the file pointer will be at the beginning of the file
- if (! $handle = fopen ($file _path, ' a '))
- {
- echo "Cannot open file $filename";
- Exit
- }
- Write the $somecontent to the file we opened.
- if (!fwrite ($handle, $error _content))
- {
- echo "Cannot write to file $filename";
- Exit
- }
- echo "File $filename write succeeded";
- echo "--the error record is saved!";
- Close File
- Fclose ($handle);
- } else {
- echo "File $filename not writable";
- }
- }else{
- The first thing to determine is that the file exists and is writable
- if (is_writable ($file _path))
- {
- Using Add mode to open $filename, the file pointer will be at the beginning of the file
- if (! $handle = fopen ($file _path, ' a '))
- {
- echo "Cannot open file $filename";
- Exit
- }
- Write the $somecontent to the file we opened.
- if (!fwrite ($handle, $error _content))
- {
- echo "Cannot write to file $filename";
- Exit
- }
- echo "File $filename write succeeded";
- echo "--the error record is saved!";
- Close File
- Fclose ($handle);
- } else {
- echo "File $filename not writable";
- }
- }
- }
- echo "
";
- if ($this->is_error) {
- Exit
- }
- }
- echo "";
- echo "
";
- echo "
";
- }
- Releasing the result set
- Public Function free () {
- @mysql_free_result ($this->result);
- }
- Database selection
- Public Function select_db ($db _database) {
- Return mysql_select_db ($db _database);
- }
- Number of query fields
- Public Function Num_fields ($table _name) {
- Return Mysql_num_fields ($this->result);
- $this->query ("select * from $table _name");
- 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->conn);
- }//function __destruct ();
- /* 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);
- }
- }
- ?>
Copy Code |