Mysql database operations from a good message book

Source: Internet
Author: User

For mysql database operations in a good message book, refer to php beginners.
Copy codeThe Code is as follows:
<? Php
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. '<br>'. $ SQL. '<br>'. $ 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. = '<meta http-equiv =" refresh "content =" 2; URL ='. $ referer. '"/> ';
$ Str. ='
<Table width = "45%" align = "center" style = "border: 1px solid # eeeeee; margin-top: 50px;">
<Tr bgcolor = "# CCCCCC">
<Td height = "25"> information prompt </td>
</Tr>
<Tr align = "center">
<Td style = "padding: 10px 0 10 p x 0"> '. $ message;
If ($ referer! = '') $ Str. = '<br/>
<Br/>
<A href = "'. $ referer.'"> if your page is not redirected, click here </a> ';

$ Str. ='
</Td>
</Tr>
</Table> ';
Echo $ str;
Exit;
}

Function showmessage ($ message, $ referer = ''){
Global $ setting, $ db;
$ Start = gettime ();
Echo <EOT
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gbk"/>
<Title> php fans guestbook _ prompt information </title>
<Link type = "text/css" href = "guest.css" rel = "stylesheet">
EOT;
If ($ referer! = '') Echo '<meta http-equiv =" Refresh "content =" 2; URL ='. $ referer. '"/> ';
Echo <EOT
</Head>
<Body>
<Center>
EOT;
Require_once('header.html ');
Echo <EOT
<Div class = "message">
<H1> message in the php fan's Guestbook {$ Message}
EOT;
If ($ referer! = ''){
Echo <EOT
<Br/>
<A href = "{$ referer}"> If your browser does not automatically Jump, click here </a>
</Div>
EOT;
}
Require_once('footer.html ');
Echo <EOT
</Center>
</Body>
</Html>
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 );
}
?>

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.