Mysql database connection

Source: Internet
Author: User

<? Php


 

The code is as follows: Copy code

$ Dbhost = 'localhost'; // database server
$ Dbuser = 'root'; // database username
$ Dbpw = 'qwaszx'; // database password
$ Dbname = 'movie '; // database name
$ Adminemail = 'Haha @ 163.com '; // system administrator Email


$ Database = 'mysql'; // cannot be modified here
$ Tplrefresh = 1; // The Automatic template refresh switch 0 = off, 1 = on
$ Pconnect = 0; // database connection method 0 = connect, 1 = pconnect

 

// ================================================ ==============================================

The code is as follows: Copy code

Class db_class {
Var $ querynum = 0;
// Function dbstuff () {global $ fp; $ fp = fopen ("./tempdata/dblog.txt", "w ");}

Function connect ($ dbhost, $ dbuser, $ dbpw, $ dbname, $ pconnect = 0 ){
If ($ pconnect ){
If (! @ Mysql_pconnect ($ dbhost, $ dbuser, $ dbpw )){
$ This-> halt ('Can not connect to MySQL server ');
   }
} Else {
If (! @ Mysql_connect ($ dbhost, $ dbuser, $ dbpw )){
$ This-> halt ('Can not connect to MySQL server ');
   }
  }
Mysql_query ("set names 'gb2312 '"); // www.111cn.net
 }

Function select_db ($ dbname ){
Return mysql_select_db ($ dbname );
 }

Function fetch_array ($ query, $ result_type = MYSQL_ASSOC ){


$ Query = mysql_fetch_array ($ query, $ result_type );

Return $ query;
 }

Function query ($ SQL, $ silence = 0 ){
// Echo "| $ SQL | <br>"; // debug
// @ Fwrite ($ GLOBALS [fp], $ SQL. ""); // debug
$ Query = mysql_query ($ SQL );
// Echo 'query: '. $ query.' <br> ';
If (! $ Query &&! $ Silence)
        {
$ This-> halt ('MySQL Query error', $ SQL );
  }
$ This-> querynum ++;
Return $ query;
 }

Function unbuffered_query ($ SQL, $ silence = 0 ){
$ Func_unbuffered_query = @ function_exists ('MySQL _ unbuffered_query ')? 'MySQL _ unbuffered_query ': 'MySQL _ query ';
$ Query = $ func_unbuffered_query ($ SQL );
If (! $ Query &&! $ Silence ){
$ This-> halt ('MySQL Query error', $ SQL );
  }
$ This-> querynum ++;
Return $ query; // www.111cn.net
 }
Function get_one ($ SQL ){
$ SQL = $ SQL;
If (function_exists ('MySQL _ unbuffered_query ')){
$ Query = mysql_unbuffered_query ($ SQL );
} Else {
$ Query = mysql_query ($ SQL );
  }
$ This-> querynum ++;
If (! $ Query) $ this-> halt ('query Error: '. $ SQL );
  
$ Rs = & mysql_fetch_array ($ query, MYSQL_ASSOC );
Mysql_free_result ($ query );
Return $ rs;
 }

Function affected_rows (){
Return mysql_affected_rows ();
 }

Function error (){
Return mysql_error ();
 }

Function errno (){
Return mysql_errno ();
 }

Function result ($ query, $ row ){
$ Query = @ mysql_result ($ query, $ row );
Return $ query; www.111cn.net
 }

Function num_rows ($ query ){
$ Query = mysql_num_rows ($ query );
Return $ query;
 }

Function num_fields ($ query ){
Return mysql_num_fields ($ query );
 }

Function free_result ($ query ){
Return mysql_free_result ($ query );
 }

Function insert_id (){
$ Id = mysql_insert_id ();
Return $ id;
 }

Function fetch_row ($ query ){
$ Query = mysql_fetch_row ($ query );
Return $ query;
 }

Function close (){
Return mysql_close ();
 }
Function halt ($ message = '', $ SQL = ''){
Echo $ message;
              }

}

/*************************************** ********************/
// ============Connect to database=================================Www.111cn.net ========================== ======================

The code is as follows: Copy code
$ Db = new db_class;
$ Db-> connect ($ dbhost, $ dbuser, $ dbpw, $ pconnect );
$ Db-> select_db ($ dbname );
Unset ($ dbhost, $ dbuser, $ dbpw, $ dbname, $ pconnect );

 

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.