Php database connection class _ PHP Tutorial

Source: Internet
Author: User
Tags php database php database connection class
A php database connection class. This article provides two database tutorial connection instances, mainly about the php Tutorial mysql tutorial data related operations, you can use it if you need it .? Phpclassmysql {private $ db_h this article provides two database tutorial connection instances, mainly about php Tutorial mysql tutorial data related operations, you can use it if you need it.

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 following provides an automatic remote or local database connection code

// Contains the mysql operation class
Include_once 'MySQL. class. php ';
// Local mysql data
$ Mysql_local_data = array ('Db _ host' => 'localhost ',
'Db _ user' => 'root ',
'Db _ pass' => 'root ',
'Db _ name' => 'test ');
// Remote mysql data
$ Mysql_remote_data = array ('Db _ host' => '61. 183.41.178 ',
'Db _ user' => 'XXX ',
'Db _ pass' => 'XXX ',
'Db _ name' => 'XXX ');
// Public data
$ Tb_prefix = 'php95 _';
$ Db_charset = 'utf-8 ';
// If the local connection is successful, the local mysql class is instantiated. Otherwise, the local database is connected and the mysql class is instantiated.
If (@ mysql_connect ($ mysql_local_data [db_host], $ mysql_local_data [db_user], $ mysql_local_data [db_pass])
$ Db = new mysql ($ db_host, $ mysql_local_data [db_user], $ mysql_local_data [db_pass], $ mysql_local_data [db_name], $ db_charset, $ conn );
Else
$ Db = new mysql ($ mysql_remote_data [db_host], $ mysql_remote_data [db_user], $ response [db_pass], $ mysql_remote_data [db_name], $ db_charset, $ conn );
$ Db-> show_tables (); // test: displays the names of all tables in the current database.
?>

Suppose we want to test. when operating the VM database in the PHP file, you must first debug it locally. Therefore, you must connect to two different databases locally and remotely. question: how to make test. does php automatically identify whether to connect to a local database or a remote database?

Prepare mysql tutorials on data-related operations. if you need them, you can use them. ? Php class mysql {private $ db_h...

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.