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 );
?>