Some classic PHP classes

Source: Internet
Author: User

MYSQL, the paging class is the most common class for writing PHP code.

Today, I watched the PHP100 video. The more I used it, the more I found that the two classes in the video are very powerful. Although I still feel that there are limitations, I have developed a mature method.

MYSQL and paging.

 

 

MYSQL:

<? Php <br/> class mysql {<br/> private $ db_host; // Database Host <br/> private $ db_user; // database username <br/> private $ db_pwd; // database username and password <br/> private $ db_database; // database name <br/> private $ conn; // database connection ID; <br/> private $ result; // result resource ID for executing the query command <br/> private $ SQL; // SQL Execution statement <br/> private $ row; // number of returned entries <br/> private $ coding; // database encoding, GBK, UTF8, gb2312 <br/> private $ bulletin = true; // enable error records <br/> private $ show_error = true; // During the test phase, all errors are displayed, which has security risks. The default setting is disabled. <br/> private $ is_error = false; // determines whether an error is terminated immediately. The default value is true, it is not recommended to enable, it is annoying to see nothing when there is a problem. </p> <p>/* constructor */<br/> public function _ construct ($ db_host, $ db_user, $ db_pwd, $ db_database, $ conn, $ coding) {<br/> $ this-> db_host = $ db_host; <br/> $ this-> db_user = $ db_user; <br/> $ this-> db_pwd = $ db_pwd; <br/> $ this-> db_database = $ db_database; <br/> $ this-> conn = $ conn; <br/> $ this-> coding = $ co Ding; <br/> $ this-> connect (); <br/>}</p> <p>/* database connection */<br/> public function connect () {<br/> if ($ this-> conn = "pconn ") {<br/> // permanent link <br/> $ this-> conn = mysql_pconnect ($ this-> db_host, $ this-> db_user, $ this-> db_pwd ); <br/>} else {<br/> // even if the link <br/> $ this-> conn = mysql_connect ($ this-> db_host, $ this-> db_user, $ this-> db_pwd); <br/>}</p> <p> if (! Mysql_select_db ($ this-> db_database, $ this-> conn) {<br/> if ($ this-> show_error) {<br/> $ this-> show_error ("database unavailable:", $ this-> db_database ); <br/>}< br/> mysql_query ("set names $ this-> coding "); <br/>}</p> <p>/* database execution statement, execute query, add, modify, delete, and other SQL statements */<br/> public function query ($ SQL) {<br/> if ($ SQL = "") {<br/> $ this-> show_error ("SQL statement error:", "SQL query statement is empty "); <br/>}< br/> $ this-> SQL = $ SQL; </p> <p> $ result = mysql _ Query ($ this-> SQL, $ this-> conn); </p> <p> if (! $ Result) {<br/> // used in debugging, the SQL statement is automatically printed when an error occurs. <br/> if ($ this-> show_error) {<br/> $ this-> show_error ("error SQL statement:", $ this-> SQL ); <br/>}< br/>}else {<br/> $ this-> result = $ result; <br/>}< br/> return $ this-> result; <br/>}</p> <p>/* Create and add a new database */<br/> public function create_database ($ database_name) {<br/> $ database = $ database_name; <br/> $ sqlDatabase = 'create database '. $ database; <br/> $ this-> query ($ sqlDatabase); <Br/>}</p> <p>/* query all the databases on the server */<br/> // separate the system database from the user database for more intuitive display? <Br/> public function show_databases () {<br/> $ this-> query ("show databases"); <br/> echo "existing database :". $ amount = $ this-> db_num_rows ($ rs); <br/> echo "<br/>"; <br/> $ I = 1; <br/> while ($ row = $ this-> fetch_array ($ rs) {<br/> echo "$ I $ row [Database]"; <br/> echo "<br/>"; <br/> $ I ++; <br/>}</p> <p> // returns all database names in the host in an array. <br/> public function databases () {<br/> $ rsPtr = mysql_list_dbs ($ this-> conn); <br/> $ I = 0; <br/> $ cnt = mysql_num_rows ($ rsPtr); <br/> while ($ I <$ cnt) {<br/> $ rs [] = mysql_db_name ($ rsPtr, $ I); <br/> $ I ++; <br/>}< br/> return $ rs; <br/>}</p> <p>/* query all tables in the database */<br/> public function show_tables ($ database_name) {<br/> $ this-> query ("show tables"); <br/> echo "existing database :". $ amount = $ this-> db_num_rows ($ rs); <br/> echo "<br/>"; <br/> $ I = 1; <br/> while ($ row = $ this-> fetch_array ($ rs) {<br/> $ column Name = "Tables_in _". $ database_name; <br/> echo "$ I $ row [$ columnName]"; <br/> echo "<br/>"; <br/> $ I ++; <br/>}</p> <p>/* <br/> mysql_fetch_row () array $ row [0], $ row [1], $ row [2] <br/> mysql_fetch_array () array $ row [0] or $ row [id] <br/> mysql_fetch_assoc () array is case sensitive with the $ row-> content field <br/> mysql_fetch_object () object with $ row [id], $ row [content] field is case sensitive <br/> */</p> <p>/* obtain result data */<br/> public function mysql _ Result_li () {<br/> return mysql_result ($ str); <br/>}</p> <p>/* obtain the record set and array-index and association, use $ row ['content'] */<br/> public function fetch_array () {<br/> return mysql_fetch_array ($ this-> result ); <br/>}</p> <p> // gets the associated array, using $ row ['field name'] <br/> public function fetch_assoc () {<br/> return mysql_fetch_assoc ($ this-> result); <br/>}</p> <p> // obtain the numeric index array, use $ row [0], $ row [1], $ row [2] <br/> public function fetch_row () {<br/> return my SQL _fetch_row ($ this-> result); <br/>}</p> <p> // get the object array, use $ row-> content <br/> public function fetch_Object () {<br/> return mysql_fetch_object ($ this-> result ); <br/>}</p> <p> // simplified query select <br/> public function findall ($ table) {<br/> $ this-> query ("SELECT * FROM $ table "); <br/>}</p> <p> // simplified query select <br/> public function select ($ table, $ columnName = "*", $ condition = '', $ debug ='') {<br/> $ condition = $ conditi On? 'Where '. $ condition: NULL; <br/> if ($ debug) {<br/> echo "SELECT $ columnName FROM $ table $ condition "; <br/>} else {<br/> $ this-> query ("SELECT $ columnName FROM $ table $ condition "); <br/>}</p> <p> // delete del <br/> public function delete ($ table, $ condition, $ url = '') {<br/> if ($ this-> query (" delete from $ table WHERE $ condition ") {<br/> if (! Empty ($ url) <br/> $ this-> Get_admin_msg ($ url, 'deleted successfully! '); <Br/>}</p> <p> // insert is simplified. <br/> public function insert ($ table, $ columnName, $ value, $ url = '') {<br/> if ($ this-> query (" insert into $ table ($ columnName) VALUES ($ value )")) {<br/> if (! Empty ($ url) <br/> $ this-> Get_admin_msg ($ url, 'added successfully! '); <Br/>}</p> <p> // simplified update <br/> public function update ($ table, $ mod_content, $ condition, $ url = '') {<br/> // echo" UPDATE $ table SET $ mod_content WHERE $ condition "; exit (); <br/> if ($ this-> query ("UPDATE $ table SET $ mod_content WHERE $ condition") {<br/> if (! Empty ($ url) <br/> $ this-> Get_admin_msg ($ url ); <br/>}</p> <p>/* obtain the ID generated by the previous INSERT operation */<br/> public function insert_id () {<br/> return mysql_insert_id (); <br/>}</p> <p> // point to a specific data record. <br/> public function db_data_seek ($ id) {<br/> if ($ id> 0) {<br/> $ id = $ id-1; <br/>}< br/> if (! @ Mysql_data_seek ($ this-> result, $ id) {<br/> $ this-> show_error ("SQL statement error:", "specified data is empty "); <br/>}< br/> return $ this-> result; <br/>}</p> <p> // calculate the number of result sets based on the select query results. <br/> public function db_num_rows () {<br/> if ($ this-> result = null) {<br/> if ($ this-> show_error) {<br/> $ this-> show_error ("SQL statement error", "temporarily empty, no content! "); <Br/>}< br/>}else {<br/> return mysql_num_rows ($ this-> result ); <br/>}</p> <p> // obtain the number of affected rows based on insert, update, and delete execution results. <br/> public function db_affected_rows () {<br/> return mysql_affected_rows (); <br/>}</p> <p> // The SQL statement is displayed. <br/> public function show_error ($ message = "", $ SQL = "") {<br/> if (! $ SQL) {<br/> echo "<font color = 'red'> ". $ message. "</font>"; <br/> echo "<br/>"; <br/>}else {<br/> echo "<fieldset> "; <br/> echo "<legend> error message: </legend> <br/>"; <br/> echo "<div style = 'font-size: 14px; clear: both; font-family: Verdana, Arial, Helvetica, sans-serif; '> "; <br/> echo" <div style = 'height: 20px; background: #000000; border: 1px #000000 solid '> "; <br/> echo" <font color = 'white'> error code: 12142 </fon T> "; <br/> echo" </div> <br/> "; <br/> echo" error cause :". mysql_error (). "<br/>"; <br/> echo "<div style = 'height: 20px; background: # FF0000; border: 1px # FF0000 solid '> "; <br/> echo" <font color = 'white'> ". $ message. "</font>"; <br/> echo "</div>"; <br/> echo "<font color = 'red'> <pre> ". $ SQL. "</pre> </font>"; <br/> $ ip = $ this-> getip (); <br/> if ($ this-> bulletin) {<br/> $ time = date ("Y-m-d H: I: s"); <br/> $ Message = $ message. "/r/n $ this-> SQL ". "/r/n customer IP Address: $ ip ". "/r/n time: $ time ". "/r/n"; </p> <p> $ server_date = date ("Y-m-d "); <br/> $ filename = $ server_date. ". txt "; <br/> $ file_path =" error /". $ filename; <br/> $ error_content = $ message; <br/> // $ error_content = "wrong database, cannot be linked "; <br/> $ file = "error"; // set the file storage directory </p> <p> // create a folder <br/> if (! File_exists ($ file) {<br/> if (! Mkdir ($ file, 0777) {<br/> // the default mode is 0777, means the maximum possible access <br/> die ("upload files directory does not exist and creation failed "); <br/>}</p> <p> // create a txt date file <br/> if (! File_exists ($ file_path) {</p> <p> // echo "create date file"; <br/> fopen ($ file_path, "w + "); </p> <p> // first, make sure the file exists and can be written. <br/> if (is_writable ($ file_path )) {<br/> // open $ filename in add mode. The file pointer starts with the file. <br/> if (! $ Handle = fopen ($ file_path, 'A') {<br/> echo "the file $ filename cannot be opened"; <br/> exit; <br/>}</p> <p> // write $ somecontent to the open file. <Br/> if (! Fwrite ($ handle, $ error_content) {<br/> echo "cannot be written to the file $ filename"; <br/> exit; <br/>}</p> <p> // echo "File $ filename written successfully"; </p> <p> echo "-- the error record is saved! "; </P> <p> // close the file <br/> fclose ($ handle ); <br/>} else {<br/> echo "File $ filename cannot be written "; <br/>}</p> <p>} else {<br/> // you must first confirm that the file exists and can be written. <br/> if (is_writable ($ file_path )) {<br/> // open $ filename in add mode. The file pointer starts with the file. <br/> if (! $ Handle = fopen ($ file_path, 'A') {<br/> echo "the file $ filename cannot be opened"; <br/> exit; <br/>}</p> <p> // write $ somecontent to the open file. <Br/> if (! Fwrite ($ handle, $ error_content) {<br/> echo "cannot be written to the file $ filename"; <br/> exit; <br/>}</p> <p> // echo "the file $ filename is successfully written."; <br/> echo "-- the error record is saved! "; </P> <p> // close the file <br/> fclose ($ handle ); <br/>} else {<br/> echo "File $ filename cannot be written "; <br/>}</p> <p >}< br/> echo "<br/> "; <br/> if ($ this-> is_error) {<br/> exit; <br/>}< br/> echo "</div>"; <br/> echo "</fieldset> "; </p> <p> echo "<br/> "; <br/>}</p> <p> // release the result set <br/> public function free () {<br/> @ mysql_free_result ($ this-> result ); <br/>}</p> <p> // select a database <br/> public function select_db ($ db_database) {<Br/> return mysql_select_db ($ db_database); <br/>}</p> <p> // query the number of fields <br/> public function num_fields ($ table_name) {<br/> // return mysql_num_fields ($ this-> result); <br/> $ this-> query ("select * from $ table_name "); <br/> echo "<br/>"; <br/> echo "field count :". $ total = mysql_num_fields ($ this-> result); <br/> echo "<pre>"; <br/> for ($ I = 0; $ I <$ total; $ I ++) {<br/> print_r (mysql_fetch_field ($ this-> result, $ I); <Br/>}< br/> echo "</pre>"; <br/> echo "<br/> "; <br/>}</p> <p> // obtain MySQL Server Information <br/> public function mysql_server ($ num = '') {<br/> switch ($ num) {<br/> case 1: <br/> return mysql_get_server_info (); // MySQL Server Information <br/> break; </p> <p> case 2: <br/> return mysql_get_host_info (); // obtain MySQL host information <br/> break; </p> <p> case 3: <br/> return mysql_get_client_info (); // obtain MySQL client information <br/> break; </p> <p> case 4: <br/> Return mysql_get_proto_info (); // obtain MySQL protocol information <br/> break; </p> <p> default: <br/> return mysql_get_client_info (); // obtain mysql version information by default <br/>}</p> <p> // destructor to automatically close the database, garbage collection mechanism <br/> public function _ destruct () {<br/> if (! Empty ($ this-> result) {<br/> $ this-> free (); <br/>}< br/> mysql_close ($ this-> conn ); <br/>}// function _ destruct (); </p> <p>/* obtain the real IP address of the client */<br/> function getip () {<br/> if (getenv ("HTTP_CLIENT_IP") & strcasecmp (getenv ("HTTP_CLIENT_IP"), "unknown ")) {<br/> $ ip = getenv ("HTTP_CLIENT_IP"); <br/>}else <br/> if (getenv ("HTTP_X_FORWARDED_FOR ") & strcasecmp (getenv ("HTTP_X_FORWARDED_FOR"), "unknown") {<br /> $ Ip = getenv ("HTTP_X_FORWARDED_FOR"); <br/>}else <br/> if (getenv ("REMOTE_ADDR ") & strcasecmp (getenv ("REMOTE_ADDR"), "unknown") {<br/> $ ip = getenv ("REMOTE_ADDR "); <br/>} else <br/> if (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], "unknown ")) {<br/> $ ip = $ _ SERVER ['remote _ ADDR ']; <br/>}else {<br/> $ ip = "unknown "; <br/>}< br/> return ($ ip); <Br/>}< br/> function inject_check ($ SQL _str) {// prevent injection <br/> $ check = eregi ('select | insert | update | delete |/'| // * | /. /. // | /. // | union | into | load_file | outfile ', $ SQL _str); <br/> if ($ check) {<br/> echo "the input is invalid! "; <Br/> exit (); <br/>}else {<br/> return $ SQL _str; <br/>}< br/> function checkurl () {// check the path <br/> if (preg_replace ("/https?: // ([^/: //] +). */I "," // 1 ", $ _ SERVER ['HTTP _ referer'])! = Preg_replace ("/([^/:] + ). */"," // 1 ", $ _ SERVER ['HTTP _ host']) {<br/> header (" Location: http://www.kebeke.com "); <br/> exit (); <br/>}</p> <p >}< br/>?> <Br/>

 

 

Paging:

<? Php <br/>/* <br/> * Created on 2007-6-8 <br/> * Programmer: Alan, msn-haowubai@hotmail.com <br/> * php100.com Develop a project PHP-MySQL-Apache <br/> * Window-Preferences-PHPeclipse-PHP-Code Templates <br/> */<br //> // to avoid errors caused by repeated file inclusion, added conditions for determining whether a function exists: <br/> $ page =$ _ GET [page]; <br/> if (! Function_exists (pageft) {<br/> // defines the function pageft (). The meaning of the three parameters is: <br/> // $ totle: total information; <br/> // $ displaypg: the number of information displayed on each page. The default value is 20. <br/> // $ url: the link in the navigation bar, except for adding different Query Information "page", all the parts are the same as the URL. <Br/> // The default value should be the URL of the current page ($ _ SERVER ["REQUEST_URI"]), but the default value can only be a constant on the right, therefore, this default value is set to an empty string and the URL of this page is set inside the function. <Br/> function pageft ($ totle, $ displaypg = 20, $ url = '') {</p> <p> // defines several global variables: <br/> // $ page: Current page number; <br/> // $ firstcount: (database) The start item of the query. <br/> // $ pagenav: page navigation bar code, which is not output by the function. <br/> // $ _ SERVER: required to read the URL "$ _ SERVER [" REQUEST_URI "]" on this page. <Br/> global $ page, $ firstcount, $ pagenav, $ _ SERVER; </p> <p> // to enable external function access to "$ displaypg ", set it as a global variable. Note that after a variable is redefined as a global variable, the original value is overwritten, so the value is assigned again here. <Br/> $ GLOBALS ["displaypg"] = $ displaypg; </p> <p> if (! $ Page) $ page = 1; </p> <p> // if $ url uses the default value, that is, null value, the value is assigned to the URL of this page: <br/> if (! $ Url) {$ url = $ _ SERVER ["REQUEST_URI"] ;}</p> <p> // URL analysis: <br/> $ parse_url = parse_url ($ url); <br/> $ url_query = $ parse_url ["query"]; // retrieve the URL query string <br/> if ($ url_query) {<br/> // because the URL may contain page number information, we need to remove it, to add new page number information. <Br/> // The regular expression is used here. See "Regular Expression in PHP" <br/> $ url_query = ereg_replace ("(^ | &) page = $ page "," ", $ url_query); </p> <p> // Replace the query string of the processed URL with the query string of the original URL: <br/> $ url = str_replace ($ parse_url ["query"], $ url_query, $ url); </p> <p> // Add page after URL to query information, but to be assigned: <br/> if ($ url_query) $ url. = "& page"; else $ url. = "page"; <br/>}else {<br/> $ url. = "? Page "; <br/>}</p> <p> // page number calculation: <br/> $ lastpg = ceil ($ totle/$ displaypg ); // The last page is also the total number of pages <br/> $ page = min ($ lastpg, $ page); <br/> $ prepg = $ page-1; // previous page <br/> $ nextpg = ($ page = $ lastpg? 0: $ page + 1); // next page <br/> $ firstcount = ($ page-1) * $ displaypg; </p> <p> // start the page navigation bar code: <br/> $ pagenav = "display no. <B> ". ($ totle? ($ Firstcount + 1): 0 ). "</B>-<B> ". min ($ firstcount + $ displaypg, $ totle ). "</B> records, a total of $ totle records"; </p> <p> // if there is only one page, the function jumps out: <br/> if ($ lastpg <= 1) return false; </p> <p> $ pagenav. = "<a href =" $ url = 1 "mce_href =" $ url = 1 "> homepage </a>"; <br/> if ($ prepg) $ pagenav. = "<a href =" $ url = $ prepg "mce_href =" $ url = $ prepg "> previous page </a>"; else $ pagenav. = "Previous Page"; <br/> if ($ nextpg) $ pagenav. = "<a href =" $ url = $ nextpg "mce_href =" $ url = $ nextpg "> next page </a>"; e Lse $ pagenav. = "back page"; <br/> $ pagenav. = "<a href =" $ url = $ lastpg "mce_href =" $ url = $ lastpg "> last page </a> "; </p> <p> // The drop-down Jump list. All page numbers are displayed cyclically: <br/> $ pagenav. = "to the <select name = 'topage' size = '1' onchange = 'window. location =/"$ url =/" + this. value'>/n "; <br/> for ($ I = 1; $ I <= $ lastpg; $ I ++) {<br/> if ($ I = $ page) $ pagenav. = "<option value = '$ I' selected> $ I </option>/n"; <br/> else $ pagenav. = "<option value = '$ I'> $ I </option>/n"; <br/>}< br/> $ Pagenav. = "</select> page, $ lastpg page in total"; <br/>}< br/>?> </P> <p>

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.