Php entry-a class connecting to the mysql database

Source: Internet
Author: User
Php is a class used to connect to the mysql database. for php learning, refer

Project structure:

Running effect;


Conn. php
Copy codeThe code is as follows:
Class ConnectionMySQL {
// Host
Private $ host = "localhost ";
// Database username
Private $ name = "root ";
// Password of the database
Private $ pass = "";
// Database name
Private $ table = "phptest ";
// Encoding format
Private $ ut = "UTF-8 ";
// Constructor
Function _ construct (){
$ This-> ut = $ ut;
$ This-> connect ();
}
// Database link
Function connect (){
$ Link = mysql_connect ($ this-> host, $ this-> name, $ this-> pass) or die ($ this-> error ());
Mysql_select_db ($ this-> table, $ link) or die ("no database:". $ this-> table );
Mysql_query ("set names '$ this-> ut '");
}
Function query ($ SQL, $ type = ''){
If (! ($ Query = mysql_query ($ SQL) $ this-> show ('say: ', $ SQL );
Return $ query;
}
Function show ($ message = '', $ SQL = ''){
If (! $ SQL) echo $ message;
Else echo $ message .'
'. $ SQL;
}
Function affected_rows (){
Return mysql_affected_rows ();
}
Function result ($ query, $ row ){
Return mysql_result ($ query, $ row );
}
Function num_rows ($ query ){
Return @ mysql_num_rows ($ query );
}
Function num_fields ($ query ){
Return mysql_num_fields ($ query );
}
Function free_result ($ query ){
Return mysql_free_result ($ query );
}
Function insert_id (){
Return mysql_insert_id ();
}
Function fetch_row ($ query ){
Return mysql_fetch_row ($ query );
}
Function version (){
Return mysql_get_server_info ();
}
Function close (){
Return mysql_close ();
}
// Insert a value to the $ table
Function fn_insert ($ table, $ name, $ value ){
$ This-> query ("insert into $ table ($ name) value ($ value )");
}
// Delete a record in the table $ table according to the value of $ id
Function fn_delete ($ table, $ id, $ value ){
$ This-> query ("delete from $ table where $ id = $ value ");
Records whose echo "id is". $ id. "are successfully deleted! ";
}
}
$ Db = new ConnectionMySQL ();
$ Db-> fn_insert ('test', 'id, name, sex ', "'', 'hongtenzone', 'M '");
$ Db-> fn_delete ('test', 'id', 1 );
?>

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.