Mysql database operations from a good message book. From a good message book for mysql database operations, php beginners can refer to the following code to copy the code :? Phpclassmysql {var $ querynum0; functionconnect ($ dbhost, $ dbu mysql database operation class obtained from a good message book. for php beginners, refer
The code is as follows:
Class mysql {
Var $ querynum = 0;
Function connect ($ dbhost, $ dbuser, $ dbpw, $ dbname = '', $ dbcharset = ''){
If (! @ Mysql_connect ($ dbhost, $ dbuser, $ dbpw )){
$ This-> show ('Can not connect to MySQL server ');
Return false;
}
If ($ dbname ){
$ This-> select_db ($ dbname );
}
If ($ this-> version ()> '4. 1' & $ dbcharset ){
$ This-> query ("set names '". $ dbcharset ."'");
}
Return true;
}
Function select_db ($ dbname ){
Return mysql_select_db ($ dbname );
}
Function fetch_array ($ query, $ result_type = MYSQL_ASSOC ){
Return @ mysql_fetch_array ($ query, $ result_type );
}
Function query ($ SQL, $ type = ''){
If (! ($ Query = mysql_query ($ SQL) $ this-> show ('MySQL Query error', $ SQL );
$ This-> querynum ++;
Return $ query;
}
Function affected_rows (){
Return mysql_affected_rows ();
}
Function result ($ query, $ row ){
Return mysql_result ($ query, $ row );
}
Function num_rows ($ query ){
Return @ mysql_num_rows ($ query );
}
Function num_fields ($ query ){
Return mysql_num_fields ($ query );
}
Function free_result ($ query ){
Return mysql_free_result ($ query );
}
Function insert_id (){
Return mysql_insert_id ();
}
Function fetch_row ($ query ){
Return mysql_fetch_row ($ query );
}
Function version (){
Return mysql_get_server_info ();
}
Function close (){
Return mysql_close ();
}
Function error (){
Return mysql_error ();
}
Function show ($ message = '', $ SQL = ''){
If (! $ SQL) echo $ message;
Else echo $ message .'
'. $ SQL .'
'. $ This-> error ();
}
}
Class page extends mysql {
Function pagination ($ SQL, $ maxnum, $ page, $ maxpages, $ pagepre, $ ext = ''){
Global $ sum, $ stail, $ link, $ lmid, $ ltail, $ curpage; // $ ext = '& class = 3'
$ SELF = $ _ SERVER ['php _ SELF '];
$ Query = $ this-> query ($ SQL );
$ Rows = $ this-> fetch_array ($ query, MYSQL_NUM );
$ Totalrows = $ rows [0];
$ Totalpages = ceil ($ totalrows/$ maxnum );
$ Startnum = ($ page-1) * $ maxnum;
$ String = $ sum. $ totalrows. $ stail. $ sum. $ page. "/". $ totalpages. $ stail;
If ($ page! = 1 ){
$ String. = $ link. $ SELF ."? Page = 1 ". $ ext. $ lmid." | Updated ". $ ltail;
$ String. = $ link. $ SELF .'? Page = '. ($ page-1). $ ext. $ lmid. "commandid". $ ltail;
}
If ($ maxpages >=$ totalpages ){
$ Pgstart = 1; $ pgend = $ totalpages;
}
Elseif ($ page-$ pagepre-1 + $ maxpages)> $ totalpages ){
$ Pgstart = $ totalpages-$ maxpages + 1;
$ Pgend = $ totalpages;
}
Else {
$ Pgstart = ($ page <= $ pagepre )? 1 :( $ page-$ pagepre ));
$ Pgend = ($ pgstart = 1 )? $ Maxpages :( $ pgstart + $ maxpages-1 ));
}
For ($ pg = $ pgstart; $ pg <= $ pgend; $ pg ++ ){
If ($ pg = $ page ){
$ String. = $ curpage. $ SELF ."? Page = ". $ pg. $ ext. $ lmid. $ pg. $ ltail;
}
Else $ string. = $ link. $ SELF ."? Page = ". $ pg. $ ext. $ lmid. $ pg. $ ltail;
}
If ($ page! = $ Totalpages ){
$ String. = $ link. $ SELF .'? Page = '. ($ page + 1). $ ext. $ lmid. "›". $ ltail;
$ String. = $ link. $ SELF .'? Page = '. $ totalpages. $ ext. $ lmid. "› |". $ ltail;
}
Return $ string;
}
}
Function html ($ str, $ allowhtml = 0 ){
$ Str = get_magic_quotes_gpc ()? $ Str: addslashes ($ str );
If (! $ Allowhtml) {$ str = htmlspecialchars ($ str );}
Return $ str;
}
Function dehtml ($ str, $ allowhtml = 0 ){
$ Str = stripslashes ($ str );
If (! $ Allowhtml) {$ str = nl2br ($ str );}
Return $ str;
}
Function deip ($ str ){
$ Arr = explode ('.', $ str );
$ Str = $ arr [0]. '.'. $ arr [1]. '.'. $ arr [2]. '. *';
Return $ str;
}
Function setting ($ login = false ){
Global $ db, $ mydbpre;
If ($ login) $ SQL = "select * from {$ mydbpre} setting ";
Else $ SQL = "select * from {$ mydbpre} setting where keyword! = 'Username' and keyword! = 'Password '";
$ Query = $ db-> query ($ SQL );
While ($ row = $ db-> fetch_array ($ query )){
$ Arr [$ row ['keyword'] = $ row ['Val'];
If ($ row ['keyword'] = 'name') $ arr ['title'] = $ row ['Val']. '-Powered by PHPfans ';
}
Return $ arr;
}
Function checkcontent ($ content ){
Global $ db, $ mydbpre, $ mearr;
$ Arrword = $ arrip = array ();
$ SQL = "select * from {$ mydbpre} ban where B _type! = 0 ";
$ Query = $ db-> query ($ SQL );
While ($ row = $ db-> fetch_array ($ query )){
If ($ row ['B _ type'] = 1) $ arrword [] = $ row [' B _ VAL'];
Elseif ($ row ['B _ type'] = 2) $ arrip [] = $ row [' B _ reval '];
}
Foreach ($ arrip as $ value ){
$ Value = preg_quote ($ value ,'/');
$ Value = str_replace ('\ *', '\ d {1, 3}', $ value );
If (preg_match ('/^ ('. $ value. ') $/', $ _ SERVER ['remote _ ADDR ']) {
Showmessage ($ mearr [8], 'index. php ');
}
}
Foreach ($ arrword as $ val ){
$ Val = preg_quote ($ val ,'/');
If (preg_match ('/'. $ val. '/', $ content )){
Showmessage ($ mearr [7], 'index. php ');
}
}
}
Function replaceword ($ content ){
Global $ db, $ mydbpre;
$ SQL = "select * from {$ mydbpre} ban where B _type = 0 ";
$ Query = $ db-> query ($ SQL );
While ($ row = $ db-> fetch_array ($ query )){
$ Content = str_replace ($ row ['B _ val'], $ row ['B _ reval'], $ content );
}
Return $ content;
}
Function showadmin ($ message, $ referer = ''){
$ Str = '';
If ($ referer! = '') $ Str. =' ';
$ Str. ='
Information prompt |
'. $ Message; If ($ referer! = '') $ Str. =' If your page does not jump, click here ';
$ Str. =' |
';
Echo $ str;
Exit;
}
Function showmessage ($ message, $ referer = ''){
Global $ setting, $ db;
$ Start = gettime ();
Echo <
Php fan guestbook _ prompt information
EOT;
If ($ referer! = '') Echo' ';
Echo <
EOT;
Require_once('header.html ');
Echo <
Prompt information in the php fan's message book
{$ Message}
EOT;
If ($ referer! = ''){
Echo <
If your browser does not automatically jump, click here
EOT;
}
Require_once('footer.html ');
Echo <
EOT;
Exit;
}
Function gettime ()
{
$ T = explode ("", microtime ());
Return $ t [1] + $ t [0];
}
Function checklogin ($ exit = true ){
If (! Isset ($ _ COOKIE ['islogin']) | $ _ COOKIE ['islogin']! = 1 ){
If ($ exit ){
Echo "Please log in first. thank you. ";
Exit;
}
}
Else setcookie ('islogin', 1, time () + 60*20 );
}
?>
The pipeline code is as follows :? Php classmysql {var $ querynum = 0; functionconnect ($ dbhost, $ dbu...