Complete php mysql Database Class

Source: Internet
Author: User
Tags php mysql
<? Phpclass mysql {private $ db_host; // Database Host private $ db_user; // database username private $ db_pwd; // database username and password private $ db_database; // database name private $ conn; // database connection ID; private $ result; // result resource ID for executing the query command private $ SQL; // SQL Execution statement private $ row; // The number of returned items private $ coding; // database encoding, GBK, UTF8, gb2312private $ bulletin = true; // whether to enable the error record private $ show_error = false; // during the test phase, all errors are displayed, which has security risks. private $ is_error = false is disabled by default. // if an error is found to be terminated immediately, tr is used by default. Ue, we recommend that you do not enable it, because when there is a problem, the user will not be able to see anything, It is very distressing/* constructor */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 link $ this-> conn = mysql_pconnect ($ th Is-> db_host, $ this-> db_user, $ this-> db_pwd);} else {// even if the link $ 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, execute query, add, modify, delete, and other SQL statements */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. When an SQL statement fails, it is automatically printed out 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 all the databases on the server * // separate the system database from the user database for more intuitive display? Public function show_databases () {$ this-> query ("show databases"); echo "existing database :". $ amount = $ this-> db_num_rows ($ rs); echo "<br/>"; $ I = 1; while ($ row = $ this-> fetch_array ($ rs) {echo "$ I $ row [Database]"; echo "<br/> "; $ I ++ ;}/// returns the public function databases () {$ rsPtr = mysql_list_dbs ($ this-> conn) of all database names in the host in an array ); $ I = 0; $ cnt = mysql_num_rows ($ rsPtr); while ($ I <$ cnt) {$ rs [] = mysql_db_name ($ rsPtr, $ I ); $ I ++;} Return $ rs;}/* query all tables in the database */public function show_tables ($ database_name) {$ this-> query ("show tables "); echo "existing database :". $ 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] or $ row [id] mys Ql_fetch_assoc () array $ row-> content field case sensitive mysql_fetch_object () object $ row [id], $ row [content] field is case sensitive * // * obtain result data */public function mysql_result_li () {return mysql_result ($ str);}/* Get Record set, obtain the array-index and association. use $ row ['content'] */public function fetch_array ($ resultt = "") {if ($ resultt <> "") {return mysql_fetch_array ($ resultt);} else {return mysql_fetch_array ($ this-> result) ;}// gets the associated array, using $ row ['field name'] public function fet Ch_assoc () {return mysql_fetch_assoc ($ this-> result);} // retrieves a numeric index array, using $ row [0], $ row [1], $ row [2] public function fetch_row () {return mysql_fetch_row ($ this-> result);} // gets the object array, using $ row-> contentpublic function fetch_Object () {return mysql_fetch_object ($ this-> result);} // simplify the query selectpublic function findall ($ table) {$ this-> query ("SELECT * FROM $ table ");} // simplify the selectpublic 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") ;}// delete delpublic function delete ($ table, $ condition, $ url = '') {if ($ this-> query (" delete from $ table WHERE $ condition ") {if (! Empty ($ url) $ this-> Get_admin_msg ($ url, 'deleted successfully! ') ;}} // Insert insertpublic function insert ($ table, $ columnName, $ value, $ url = '') {if ($ this-> query ("insert into $ table ($ columnName) VALUES ($ value)") {if (! Empty ($ url) $ this-> Get_admin_msg ($ url, 'added successfully! ') ;}} // Simplify updatepublic 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) ;}/ * Get the ID generated by the previous INSERT operation */public function insert_id () {return mysql_insert_id ();} // point to a specific data record. 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 the number of result sets according to the 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) ;}// obtain the public function db_affected_rows () according to the insert, update, and delete execution results () {return mysql_affected_rows ();} // The SQL statement public function show_error ($ message = "", $ SQL = "") {if (! $ SQL) {echo "<font color = 'red'> ". $ message. "</font>"; echo "<br/>";} else {echo "<fieldset>"; echo "<legend> error message: </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'> error code: 12142 </font>"; echo "</div> <br/>"; echo "error cause :". mysql_err Or (). "<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. "\ r \ n $ this-> SQL ". "\ r \ n customer IP: $ ip ". "\ r \ n time: $ time ". "\ r \ n"; $ server_date = date ("Y-m-d"); $ filename = $ server_date. ". txt "; $ file_path =" error /". $ filename; $ error_content = $ message; // $ error_content = "wrong database, cannot be linked"; $ file = "error "; // set the file storage directory // create a folder if (! File_exists ($ file) {if (! Mkdir ($ file, 0777) {// the default mode is 0777, which means the most likely access to die ("upload files directory does not exist and creation failed ");}} // create a txt date file if (! File_exists ($ file_path) {// echo "create date file"; fopen ($ file_path, "w + "); // first, make sure that the file exists and can be written if (is_writable ($ file_path) {// open $ filename in add mode. The file pointer will start with if (! $ Handle = fopen ($ file_path, 'A') {echo "cannot open the file $ filename"; exit ;}// write $ somecontent to the file we open. If (! Fwrite ($ handle, $ error_content) {echo "the file $ filename"; exit;} // echo "the file $ filename is successfully written "; echo "-- the error record is saved! "; // Close the file fclose ($ handle);} else {echo" File $ filename cannot be written ";}} else {// first make sure that the file exists and can be written if (is_writable ($ file_path) {// open $ filename in add mode. The file pointer will start with if (! $ Handle = fopen ($ file_path, 'A') {echo "cannot open the file $ filename"; exit ;}// write $ somecontent to the file we open. If (! Fwrite ($ handle, $ error_content) {echo "the file $ filename"; exit;} // echo "the file $ filename is successfully written "; echo "-- the error record is saved! "; // Close the file fclose ($ handle);} else {echo" File $ filename cannot be written ";}}} echo" <br/> "; if ($ this-> is_error) {exit ;}} echo "</div>"; echo "</fieldset>"; echo "<br/> ";} // release the public function free () {@ mysql_free_result ($ this-> result);} // select public function select_db ($ db_database) for the database) {return mysql_select_db ($ db_database);} // query the number of fields public function num_fields ($ table_name) {// return mysql_num_fields ($ this-> result); $ this-> q Uery ("select * from $ table_name"); echo "<br/>"; echo "field count :". $ 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/> ";} // obtain 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 (); // obtain M YSQL host information break; case 3: return mysql_get_client_info (); // obtain MySQL client information break; case 4: return mysql_get_proto_info (); // obtain MySQL protocol information break; default: return mysql_get_client_info (); // obtain mysql version information by default} // destructor, automatically shut down the database, public function _ destruct () {if (! Empty ($ this-> result) {$ this-> free ();} mysql_close ($ this-> conn);} // function _ destruct (); /* obtain 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");} elseif (getenv ("HTTP_X_FORWARDED_FOR") & strcasecmp (getenv ("HTTP_X_FORWARDED_FOR"), "unknown ")) {$ ip = getenv ("HTTP_X_FORWARDED_FOR");} elseif (getenv ("REMOTE_AD DR ") & strcasecmp (getenv (" REMOTE_ADDR ")," unknown ") {$ ip = getenv (" REMOTE_ADDR ");} elseif (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) {// prevent injection $ check = eregi ('select | insert | update | delete | \ '| \/\ * | \. \. \/| \. \/| union | into | load_file | Outfile ', $ SQL _str); if ($ check) {echo "the input is invalid! "; Exit () ;}else {return $ SQL _str ;}} function checkurl () {// check the if (preg_replace ("/https?: \/([^ \: \/] +). */I "," \ 1 ", $ _ SERVER ['HTTP _ referer'])! = Preg_replace ("/([^ \:] + ). */"," \ 1 ", $ _ SERVER ['HTTP _ host']) {header (" Location: http://www.dareng.com "); exit () ;}}?>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.