Jsp database connection query code

Source: Internet
Author: User
Tags character set chr mysql query pconnect

Jsp database connection query code
Define ('Db _ host', 'localhost'); // address of the database server HOST

Define ('Db _ user', 'zkh131'); // database account

Define ('Db _ PW ', 'n3a9q2e3'); // database password

Define ('Db _ name', 'zkh131 _ db'); // database NAME

Define ('Db _ pre', 'hl _ '); // database table prefix. When multiple sets of Phpcms are installed in the same database, modify the table prefix.

Define ('Db _ charset', 'gbk'); // database character set

Define ('Db _ PCONNECT ', 0); // 0 or 1, whether to use persistent connection

Define ('Db _ database', 'mysql'); // DATABASE type

$ Db = new db_mysql;

$ Db-> connect (DB_HOST, DB_USER, DB_PW, DB_NAME, DB_PCONNECT, DB_CHARSET );

Class db_mysql

{

Var $ connid;

Var $ dbname;

Var $ querynum = 0;

Var $ debug = 1;

Var $ search = array ('/union (s *(/*.**/)? S *) + select/I ','/load_file (s *(/*.**/)? S *) + (/I ','/into (s *(/*.**/)? S *) + outfile/I ');

Var $ replace = array ('Union & nbsp; Select', 'load _ file & nbsp; (', 'into & nbsp; outfile ');

 

Function connect ($ dbhost, $ dbuser, $ dbpw, $ dbname = '', $ pconnect = 0, $ charset = '')

 {

$ Func = $ pconnect = 1? 'MySQL _ pconnect ': 'MySQL _ connect ';

If (! $ This-> connid = @ $ func ($ dbhost, $ dbuser, $ dbpw ))

  {

If (DB_NAME = ''& file_exists (PHPCMS_ROOT. 'Install. Php '))

   {

Header ('Location:./install. Php ');

Exit;

   }

$ This-> halt ('Can not connect to MySQL server ');

Return false;

  }

If ($ this-> version ()> '4. 1 ')

  {

$ Serverset = $ charset? "Character_set_connection = '$ charset', character_set_results =' $ charset', character_set_client = binary ":'';

$ Serverset. = $ this-> version ()> '5. 0.1 '? (Empty ($ serverset )? '': ',')." SQL _mode = ''"):'';

$ Serverset & mysql_query ("SET $ serverset", $ this-> connid );

  }

If ($ dbname &&! @ Mysql_select_db ($ dbname, $ this-> connid ))

  {

$ This-> halt ('cannot use database'. $ dbname );

Return false;

  }

$ This-> dbname = $ dbname;

Return $ this-> connid;

 }

 

Function select_db ($ dbname)

 {

If (! @ Mysql_select_db ($ dbname, $ this-> connid) return false;

$ This-> dbname = $ dbname;

Return true;

    }

 

Function query ($ SQL, $ type = '')

 {

$ Func = $ type = 'unbuffered '? 'MySQL _ unbuffered_query ': 'MySQL _ query ';

If (! ($ Query =@ $ func ($ SQL, $ this-> connid) & $ type! = 'Silent ')

  {

$ This-> halt ('MySQL Query error', $ SQL );

Return false;

  }

$ This-> querynum ++;

Return $ query;

 }

 

Function select ($ SQL, $ keyfield = '')

 {

$ Array = array ();

$ Result = $ this-> query ($ SQL );

While ($ r = $ this-> fetch_array ($ result ))

  {

If ($ keyfield)

   {

$ Key = $ r [$ keyfield];

$ Array [$ key] = $ r;

   }

Else

   {

$ Array [] = $ r;

   }

  }

$ This-> free_result ($ result );

Return $ array;

 }

 

Function insert ($ tablename, $ array)

 {

Return $ this-> query ("insert into '$ tablename '('". implode ('','', array_keys ($ array )). "') VALUES ('". implode ("','", $ array ). "')");

 }

 

Function update ($ tablename, $ array, $ where = '')

 {

If ($ where)

  {

$ SQL = '';

Foreach ($ array as $ k => $ v)

   {

$ SQL. = ", '$ k' =' $ V '";

   }

$ SQL = substr ($ SQL, 1 );

$ SQL = "updat' $ tablename 'set $ SQL WHERE $ where ";

  }

Else

  {

$ SQL = "REPLACE INTO '$ tablename '('". implode ('','', array_keys ($ array )). "') VALUES ('". implode ("','", $ array ). "')";

  }

Return $ this-> query ($ SQL );

 }

 

Function get_primary ($ table)

 {

$ Result = $ this-> query ("show columns from $ table ");

While ($ r = $ this-> fetch_array ($ result ))

  {

If ($ r ['key'] = 'pri ') break;

  }

$ This-> free_result ($ result );

Return $ r ['field'];

 }

 

Function get_fields ($ table)

 {

$ Fields = array ();

$ Result = $ this-> query ("show columns from $ table ");

While ($ r = $ this-> fetch_array ($ result ))

  {

$ Fields [] = $ r ['field'];

  }

$ This-> free_result ($ result );

Return $ fields;

 }

 

Function get_one ($ SQL, $ type = '', $ expires = 3600, $ dbname = '')

 {

$ Query = $ this-> query ($ SQL, $ type, $ expires, $ dbname );

$ Rs = $ this-> fetch_array ($ query );

$ This-> free_result ($ query );

Return $ rs;

 }

 

Function fetch_array ($ query, $ result_type = MYSQL_ASSOC)

 {

Return mysql_fetch_array ($ query, $ result_type );

 }

 

Function affected_rows ()

 {

Return mysql_affected_rows ($ this-> connid );

 }

 

Function num_rows ($ query)

 {

Return mysql_num_rows ($ query );

 }

 

Function num_fields ($ query)

 {

Return mysql_num_fields ($ query );

 }

 

Function result ($ query, $ row)

 {

Return @ mysql_result ($ query, $ row );

 }

 

Function free_result (& $ query)

 {

Return mysql_free_result ($ query );

 }

 

Function insert_id ()

 {

Return mysql_insert_id ($ this-> connid );

 }

 

Function fetch_row ($ query)

 {

Return mysql_fetch_row ($ query );

 }

 

Function escape ($ string)

 {

If (! Is_array ($ string) return str_replace (array ('N', 'r'), array (chr (10), chr (13 )), mysql_real_escape_string (preg_replace ($ this-> search, $ this-> replace, $ string), $ this-> connid ));

Foreach ($ string as $ key => $ val) $ string [$ key] = $ this-> escape ($ val );

Return $ string;

 }

 

Function table_status ($ table)

 {

Return $ this-> get_one ("show table status like '$ table '");

 }

 

Function tables ()

 {

$ Tables = array ();

$ Result = $ this-> query ("show tables ");

While ($ r = $ this-> fetch_array ($ result ))

  {

$ Tables [] = $ r ['Tables _ in _ '. $ this-> dbname];

  }

$ This-> free_result ($ result );

Return $ tables;

 }

 

Function table_exists ($ table)

 {

$ Tables = $ this-> tables ($ table );

Return in_array ($ table, $ tables );

 }

 

Function field_exists ($ table, $ field)

 {

$ Fields = $ this-> get_fields ($ table );

Return in_array ($ field, $ fields );

 }

 

Function version ()

 {

Return mysql_get_server_info ($ this-> connid );

 }

 

Function close ()

 {

Return mysql_close ($ this-> connid );

 }

 

Function error ()

 {

Return @ mysql_error ($ this-> connid );

 }

 

Function errno ()

 {

Return intval (@ mysql_errno ($ this-> connid ));

 }

 

Function halt ($ message = '', $ SQL = '')

 {

$ This-> errormsg = "<B> MySQL Query: </B> $ SQL <br/> <B> MySQL Error: </B> ". $ this-> error (). "<br/> <B> MySQL Errno: </B> ". $ this-> errno (). "<br/> <B> Message: </B> $ message ";

If ($ this-> debug)

  {

$ Msg = (defined ('in _ admin') | DEBUG )? $ This-> errormsg: "Bad Request. $ LANG [illegal_request_return]";

Echo '<div style = "font-size: 12px; text-align: left; border: 1px solid #9cc9e0; padding: 1px 4px; color: #000000; font-family: arial, Helvetica, sans-serif; "> <span> '. $ msg. '</span> </div> ';

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.