Php connection to mysql database operation class. Php connection to mysql database operation class this is a relatively comprehensive mysql operation class. Yesterday I wrote a simple connection to the mysql database code, which is the simplest, php connection to mysql database operation class this is a relatively comprehensive mysql operation class. Yesterday I wrote a simple connection to the mysql database code, which is the simplest, this is an operation that includes data query, update, deletion, and so on.
Php connection to mysql tutorial database tutorial operation class
This is a relatively comprehensive mysql operation class. Yesterday I wrote a simple connection code to the mysql database, which is the simplest, including data query, update, delete, and other operations.
*/
Class mysql {
Private $ db_host; // database host
Private $ db_user; // database username
Private $ db_pwd; // database password
Private $ db_database; // database name
Private $ conn; // Database Connection ID;
Private $ SQL; // SQL statement executed
Private $ result; // resource ID of the query
Private $ coding; // database encoding, gbk, utf8, gb2312
Private $ show_error = true; // used for local debugging, print an error
/**
* Constructor
*
* @ Access public
* @ Parameter string $ db_host database host
* @ Parameter string $ db_user database username
* @ Parameter string $ db_pwd database password
* @ Parameter string $ db_database database name
* @ Parameter string $ coding encoding
* @ Return void
*/
Public function _ construct ($ db_host, $ db_user, $ db_pwd, $ db_database, $ coding ){
$ This-> db_host = $ db_host;
$ This-> db_user = $ db_user;
$ This-> db_pwd = $ db_pwd;
$ This-> db_database = $ db_database;
$ This-> coding = $ coding;
$ This-> connect ();
}
/**
* Link to the database
*
* @ Access private
* @ Return void
*/
Private function connect (){
$ This-> conn = @ mysql_connect ($ this-> db_host, $ this-> db_user, $ this-> db_pwd );
If (! $ This-> conn ){
// Print the error when show_error is enabled
If ($ this-> show_error ){
$ This-> show_error ('error prompt: failed to connect to the database! ');
}
}
If (! @ Mysql_select_db ($ this-> db_database, $ this-> conn )){
// Failed to open the database
If ($ this-> show_error ){
$ This-> show_error ('error prompt: failed to open the database! ');
}
}
If (! @ Mysql_query ("set names $ this-> coding ")){
// Failed to set the encoding.
If ($ this-> show_error ){
$ This-> show_error ('error prompt: failed to set the encoding! ');
}
}
}
/**
* Execute queries, add, modify, delete, and other SQL statements.
*
* @ Access public
* @ Parameter string $ SQL statement
* @ Return resource identifier
*/
Public function query ($ SQL ){
$ This-> SQL = $ SQL;
$ Result = mysql_query ($ this-> SQL, $ this-> conn );
If (! $ Result ){
// Query execution failed, print error
$ This-> show_error ("incorrect SQL statement:", $ this-> SQL );
} Else {
// Return the resource identifier
Return $ this-> result = $ result;
}
}
/**
* Query all databases on the mysql server
*
* @ Access public
* @ Return void
*/
Public function show_databases (){
$ This-> query ("show databases ");
// Print the total number of databases
Echo "existing database:". mysql_num_rows ($ this-> result );
Echo"
";
$ I = 1;
// Output the name of each database in a loop
While ($ row = mysql_fetch_array ($ this-> result )){
Echo "$ I $ row [database]"."
";
$ I ++;
}
}
/**
* Query all table names in the database
*
* @ Access public
* @ Return void
*/
Public function show_tables (){
$ This-> query ("show tables ");
// Print the total number of tables
Echo "database {$ this-> db_database} total". mysql_num_rows ($ this-> result). "table :";
Echo"
";
// Construct an array subscript to loop out all database table names
$ Column_name = "tables_in _". $ this-> db_database;
$ I = 1;
// Output the name of each table in a loop
While ($ row = mysql_fetch_array ($ this-> result )){
Echo "$ I $ row [$ column_name]"."
";
$ I ++;
}
}
/**
* Retrieve the record set, array-index and association
*
* @ Access public
* @ Return void
*/
Public function fetch_array (){
Return mysql_fetch_array ($ this-> result );
}
/**
* Simplified select query statements
*
* @ Access public
* @ Parameter string $ table name
* @ Parameter string $ field name
* @ Return resource
*/
Public function findall ($ table, $ field = '*'){
Return $ this-> query ("select $ field from $ table ");
}
/**
* Simplified delete query statements
*
* @ Access public
* @ Parameter string $ table name
* @ Parameter string $ condition query condition
* @ Return resource
*/
Public function delete ($ table, $ condition ){
Return $ this-> query ("delete from $ table where $ condition ");
}
/**
* Simplified insert statements
*
* @ Access public
* @ Parameter string $ table name
* @ Parameter string $ field name
* @ Parameter string $ value insert value
* @ Return resource
*/
Public function insert ($ table, $ field, $ value ){
Return $ this-> query ("insert into $ table ($ field) values ('$ value ')");
}
/**
* Simplified update insert statements
*
* @ Access public
* @ Parameter string $ table name
* @ Parameter string $ update_content the updated content
* @ Parameter string $ condition
* @ Return resource
*/
Public function update ($ table, $ update_content, $ condition ){
Return $ this-> query ("update $ table set $ update_content where $ condition ");
}
/**
* Obtain the id generated by the previous insert operation.
*
* @ Access public
* @ Return integer
*/
Public function insert_id (){
Return mysql_insert_id ();
}
/**
* Number of computing result sets
*
* @ Access public
* @ Return integer
*/
Public function num_rows (){
Return mysql_num_rows ($ this-> result );
}
/**
* Query the field quantity and field information.
*
* @ Access public
* @ Parameter string $ table name
* @ Return void
*/
Public function num_fields ($ table ){
$ This-> query ("select * from $ table ");
Echo"
";
// Print the number of fields
Echo "number of fields:". $ total = mysql_num_fields ($ this-> result );
Echo"
";
// The mysql_fetch_field () function retrieves column information from the result set and returns it as an object.
For ($ I = 0; $ I <$ total; $ I ++ ){
Print_r (mysql_fetch_field ($ this-> result, $ I ));
}
Echo"
";
Echo"
";
}
/**
* Output SQL statement error message
*
* @ Access public
* @ Parameter string $ message prompt information
* @ Return void
*/
Public function show_error ($ message = '', $ SQL = ''){
Echo"
";
Echo" Error message:
";
Echo"
";
// Print the cause of the error
Echo "error cause:". mysql_error ()."
";
// Print the error message
// The mysql_error () function returns the text error message generated by the previous mysql operation.
Echo"
";
Echo "". $ message ."";
Echo"
";
// Print the wrong SQL statement
Echo"
" . $sql . "
";
Echo"
";
Echo" ";
}
}
// Usage
$ Mysql = new mysql ($ dbhost, $ dbuser, $ dbpwd, $ dbname, $ coding );
?>
Http://www.bkjia.com/PHPjc/630818.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630818.htmlTechArticlephp connection mysql database operation class this is a more comprehensive mysql operation class Oh, yesterday I wrote a simple connection mysql database code, relative to this, that is the simplest ,...