Mysql database operations (version 1127, source code download) _ PHP Tutorial

Source: Internet
Author: User
Tags php download
Mysql database operation class (version 1127, which provides source code download ). The code for downloading and copying Mysql. class. php is as follows :? PhpclassMysql {private $ db_host; host address private $ db_user; user name private $ db_pass; connection password private $ db_name Mysql. class. php download

The code is as follows:


Class Mysql {
Private $ db_host; // host address
Private $ db_user; // User name
Private $ db_pass; // connection password
Private $ db_name; // name
Private $ db_charset; // encoding
Private $ conn;
Public $ debug = false; // The debug switch is disabled by default.
Private $ query_id; // used to determine whether the SQL statement is successfully executed
Private $ result; // result set
Private $ num_rows; // number of rows in the result set, which is only valid for select
Private $ insert_id; // ID generated by the previous INSERT operation
// Constructor/destructor
Function _ construct ($ db_host, $ db_user, $ db_pass, $ db_name, $ db_charset, $ conn ){
$ This-> db_host = $ db_host;
$ This-> db_user = $ db_user;
$ This-> db_pass = $ db_pass;
$ This-> db_name = $ db_name;
$ This-> db_charset = $ db_charset;
$ This-> conn = $ conn;
$ This-> connect ();
}
Function _ destruct (){
@ Mysql_close ($ this-> conn );
}
// Connect to/select a database
Public function connect (){
If ($ this-> conn = 'pconn '){
@ $ This-> conn = mysql_pconnect ($ this-> db_host, $ this-> db_user, $ this-> db_pass );
} Else {
@ $ This-> conn = mysql_connect ($ this-> db_host, $ this-> db_user, $ this-> db_pass );
}
If (! $ This-> conn ){
$ This-> show_error ('database-connection failed: username or password error! ');
}
If (! @ Mysql_select_db ($ this-> db_name, $ this-> conn )){
$ This-> show_error ("database-selection failed: Database $ this-> db_name unavailable ");
}
Mysql_query ("set names $ this-> db_charset ");
Return $ this-> conn;
}
// Query method
Public function query ($ SQL ){
If ($ this-> query_id) $ this-> free_result ();
$ This-> query_id = @ mysql_query ($ SQL, $ this-> conn );
If (! $ This-> query_id) $ this-> show_error ("SQL statement\ "$ SQL \"Execution error ");
Return $ this-> query_id;
}
// Display detailed error information
Public function show_error ($ msg ){
If ($ this-> debug ){
$ Errinfo = mysql_error ();
Echo "Error: $ msg
Return: $ errinfo

";
} Else {
Echo'

An error occurred!

';
}
}
// Obtain the query execution success or failure information
Public function get_query_info ($ info ){
If ($ this-> query_id ){
Echo $ info;
}
}
// Query all
Public function findall ($ table_name ){
$ This-> query ("select * from $ table_name ");
}
// Mysql_fetch_array
Public function fetch_array (){
If ($ this-> query_id ){
$ This-> result = mysql_fetch_array ($ this-> query_id );
Return $ this-> result;
}
}
//......
Public function fetch_assoc (){
If ($ this-> query_id ){
$ This-> result = mysql_fetch_assoc ($ this-> query_id );
Return $ this-> result;
}
}
Public function fetch_row (){
If ($ this-> query_id ){
$ This-> result = mysql_fetch_row ($ this-> query_id );
Return $ this-> result;
}
}
Public function fetch_object (){
If ($ this-> query_id ){
$ This-> result = mysql_fetch_object ($ this-> query_id );
Return $ this-> result;
}
}
// Obtain num_rows
Public function num_rows (){
If ($ this-> query_id ){
$ This-> num_rows = mysql_num_rows ($ this-> query_id );
Return $ this-> num_rows;
}
}
// Obtain insert_id
Public function insert_id (){
Return $ this-> insert_id = mysql_insert_id ();
}
// Display the total number of tables
Public function show_tables (){
$ This-> query ("show tables ");
If ($ this-> query_id ){
Echo "database $ this-> db_name total". $ this-> num_rows ($ this-> query_id). "table
";
$ I = 1;
While ($ row = $ this-> fetch_array ($ this-> query_id )){
Echo "$ I -- $ row [0]
";
$ I ++;
}
}
}
// Display the total number of databases
Public function show_dbs (){
$ This-> query ("show databases ");
If ($ this-> query_id ){
Echo "total databases". $ this-> num_rows ($ this-> query_id )."
";
$ I = 1;
While ($ this-> row = $ this-> fetch_array ($ this-> query_id )){
Echo "$ I --". $ this-> row [Database]."
";
$ I ++;
}
}
}
// Delete database: return the deletion result
Public function drop_db ($ db_name = ''){
If ($ db_name = ''){
$ Db_name = $ this-> db_name; // the current database is deleted by default.
$ This-> query ("drop database $ db_name ");
} Else {
$ This-> query ("drop database $ db_name ");
}
If ($ this-> query_id ){
Return "database $ db_name deleted ";
} Else {
$ This-> show_error ("database $ db_name deletion failed ");
}
}
// Delete a data table: return the deletion result.
Public function drop_table ($ table_name ){
$ This-> query ("drop table $ table_name ");
If ($ this-> query_id ){
Return "the data table $ table_name is successfully deleted ";
} Else {
$ This-> show_error ("An error occurred while deleting the data table $ table_name ");
}
}
// Create a database
Public function create_db ($ db_name ){
$ This-> query ("create database $ db_name ");
If ($ this-> query_id ){
Return "database $ db_name created successfully ";
} Else {
$ This-> show_error ("database $ db_name creation failed ");
}
}
// Obtain the database version
Public function get_info (){
Echo mysql_get_server_info ();
}
// Release the memory
Public function free_result (){
If (@ mysql_free_result ($ this-> query_id ))
Unset ($ this-> result );
$ This-> query_id = 0;
}
} // End class
?>


The http://www.bkjia.com/PHPjc/322615.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/322615.htmlTechArticleMysql.class.php download code is as follows :? Php class Mysql {private $ db_host; // host address private $ db_user; // User name private $ db_pass; // connection password private $ db_name...

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.