[Php] mysql database operations DB Class _ MySQL

Source: Internet
Author: User
Tags mysql tutorial pconnect
[Php] mysql database operations-DB Class /*
* Mysql database DB
* @ Package db
* @ Author yytcpt (shadow)
* @ Version 2008-03-27
* @ Copyrigth http://www.d5s.cn/
*/
Class db {
Var $ connection_id = "";
Var $ pconnect = 0;
Var $ shutdown_queries = array ();
Var $ queries = array ();
Var $ query_id = "";
Var $ query_count = 0;
Var $ record_row = array ();
Var $ failed = 0;
Var $ halt = "";
Var $ query_log = array ();
Function connect ($ db_config ){
If ($ this-> pconnect ){
$ This-> connection_id = mysql_pconnect ($ db_config ["hostname"], $ db_config ["username"], $ db_config ["password"]);
} Else {
$ This-> connection_id = mysql_connect ($ db_config ["hostname"], $ db_config ["username"], $ db_config ["password"]);
}
If (! $ This-> connection_id ){
$ This-> halt ("Can not connect MySQL Server ");
}
If (! @ Mysql_select_db ($ db_config ["database"], $ this-> connection_id )){
$ This-> halt ("Can not connect MySQL Database ");
}
If ($ db_config ["charset"]) {
@ Mysql_unbuffered_query ("set names '". $ db_config ["charset"]. "'");
}
Return true;
}
// Send an SQL query and return the result set
Function query ($ query_id, $ query_type = 'MySQL _ query '){
$ This-> query_id = $ query_type ($ query_id, $ this-> connection_id );
$ This-> queries [] = $ query_id;
If (! $ This-> query_id ){
$ This-> halt ("query failed:/n $ query_id ");
}
$ This-> query_count ++;
$ This-> query_log [] = $ str;
Return $ this-> query_id;
}
// The row that sends an SQL query and does not obtain or cache the result.
Function query_unbuffered ($ SQL = ""){
Return $ this-> query ($ SQL, 'MySQL _ unbuffered_query ');
}
// Obtain a row from the result set as an associated array
Function fetch_array ($ SQL = ""){
If ($ SQL = "") $ SQL = $ this-> query_id;
$ This-> record_row = @ mysql_fetch_array ($ SQL, MYSQL_ASSOC );
Return $ this-> record_row;
}
Function shutdown_query ($ query_id = ""){
$ This-> shutdown_queries [] = $ query_id;
}
// Obtain the number of rows in the result set. only INSERT, UPDATE, or DELETE rows are returned.
Function affected_rows (){
Return @ mysql_affected_rows ($ this-> connection_id );
}
// Obtain the number of rows in the result set, which is only valid for SELECT statements.
Function num_rows ($ query_id = ""){
If ($ query_id = "") $ query_id = $ this-> query_id;
Return @ mysql_num_rows ($ query_id );
}
// Returns the numeric encoding of the error message in the previous MySQL operation.
Function get_errno (){
$ This-> errno = @ mysql_errno ($ this-> connection_id );
Return $ this-> errno;
}
// Obtain the ID generated by the previous INSERT operation
Function insert_id (){
Return @ mysql_insert_id ($ this-> connection_id );
}
// Obtain the number of queries
Function query_count (){
Return $ this-> query_count;
}
// Release the result memory
Function free_result ($ query_id = ""){
If ($ query_id = "") $ query_id = $ this-> query_id;
@ Mysql_free_result ($ query_id );
}
// Close the MySQL connection
Function close_db (){
If ($ this-> connection_id) return @ mysql_close ($ this-> connection_id );
}
// List tables in the MySQL database
Function get_table_names (){
Global $ db_config;
$ Result = mysql_list_tables ($ db_config ["database"]);
$ Num_tables = @ mysql_numrows ($ result );
For ($ I = 0; $ I <$ num_tables; $ I ++ ){
$ Tables [] = mysql_tablename ($ result, $ I );
}
Mysql_free_result ($ result );
Return $ tables;
}
// Retrieve the column information from the result set and return it as an object to retrieve all fields
Function get_result_fields ($ query_id = ""){
If ($ query_id = "") $ query_id = $ this-> query_id;
While ($ field = mysql_fetch_field ($ query_id )){
$ Fields [] = $ field;
}
Return $ fields;
}
// Error message
Function halt ($ the_error = ""){
$ Message = $ the_error ."
/R/n ";
$ Message. = $ this-> get_errno ()."
/R/n ";
$ SQL = "INSERT INTO 'DB _ error' (pagename, errstr, timer) VALUES ('". [Php] mysql database operations-DB-webmaster College

Future Chinese webmasters will focus more on the development of China's networksAdvertisement service | latest update | add to favorites | set as Homepage

  • Homepage
  • Internet software
  • IDC products
  • Webmaster transaction
  • Project outsourcing
  • Business Information
  • Recruitment
  • Company Database
  • Webmaster Mall
  • Asset Information
  • Downlink
  • School
  • Work Tools
  • Tan

Popular keywords:For CSS layout color, see Dreamweaver8 apacheApacheIIS6AjaxWML

Homepage | HOME webmaster getting started operation planning search optimization website profit webpage design HtmlCSS/XhtmlDhtmlWAPDreamweaver design principles graphic animation photoshopFireworkFlash website development Asp. NetPhpJspMssqlAccessOracleMysql server FressbsdLINUXWindows

Video tutorial Frequently used manual Common advertisement code Js special effect code

Webmaster College> Website Development technical tutorial> mysql tutorial> [php] mysql database operations-DB Class

[Php] mysql database operations-DB Class

6:12:00 view learning experience

{GetProperty (Content )}

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.