What should I do if one line of code cannot provide the functions you want?
What should I do if one line of code cannot provide the functions you want?
The answer is to create a database class. Through the second encapsulation of functions, Hong Kong server leasing achieves very good reuse. Include it when using it.
Before talking about the PHP database, let's first introduce the key points of Mysql: You can use phpmyadmin to learn about database operations.
In phpmyadmin, you can see that encoding is all in Chinese UTF-8.
Mysql database types are mainly char (fixed space string, the size is the number of Chinese characters) and varchar (variable space string, the size is the number of Chinese characters initialized), int (how many digits is the integer), float (floating point number), timestamp (date, which can be automatically created when it is set up, and formatted date when it is output), text (text) bool (Boolean)
When writing an SQL statement, SUM () can be used to calculate the value. order by 'id' DESC LIMIT 10, 10, and so on must be usable.
In phpmyadmin, just learn how to add, delete, modify, and query SQL statements.
Instance 20 Mysql class
The Code is as follows:
Class opmysql {
Private $ host = 'localhost'; // server address
Private $ name = 'root'; // Logon account
Private $ pwd = ''; // logon Password
Private $ dBase = 'a0606123620 '; // Database Name
Private $ conn = ''; // database link resource
Private $ result = ''; // result set
Private $ msg = ''; // return results
Private $ fields; // return Field
Private $ fieldsNum = 0; // number of returned Fields
Private $ rowsNum = 0; // number of returned results
Private $ rowsRst = ''; // returns the field array of a single record.
Private $ filesArray = array (); // returns an array of fields.
Private $ rowsArray = array (); // array of returned results
Private $ idusername = array ();
Private $ idsubtitle = array ();
// Initialization class
Function _ construct ($ host = '', $ name ='', $ pwd = '', $ dBase = ''){
If ($ host! = '')
$ This-> host = $ host;
If ($ name! = '')
$ This-> name = $ name;
If ($ pwd! = '')
$ This-> pwd = $ pwd;
If ($ dBase! = '')
$ This-> dBase = $ dBase;
$ This-> init_conn ();
}
// Link to the database
Function init_conn (){
$ This-> conn = @ mysql_connect ($ this-> host, $ this-> name, $ this-> pwd );
@ Mysql_select_db ($ this-> dBase, $ this-> conn );
Mysql_query ("set names utf8 ");
}
// Query results
Function mysql_query_rst ($ SQL ){
If ($ this-> conn = ''){
$ This-> init_conn ();
}
$ This-> result = @ mysql_query ($ SQL, $ this-> conn );
}
// Obtain the number of query result Fields
Function getFieldsNum ($ SQL ){
$ This-> mysql_query_rst ($ SQL );
$ This-> fieldsNum = @ mysql_num_fields ($ this-> result );
}
// Obtain the number of query result rows
Function getRowsNum ($ SQL ){
$ This-> mysql_query_rst ($ SQL );
If (mysql_errno () = 0 ){
Return @ mysql_num_rows ($ this-> result );
} Else {
Return '';
}
}
// Retrieve the record array with an index (single record)
Function getRowsRst ($ SQL ){
$ This-> mysql_query_rst ($ SQL );
If (mysql_error () = 0 ){
$ This-> rowsRst = mysql_fetch_array ($ this-> result, MYSQL_ASSOC );
Return $ this-> rowsRst;
} Else {
Return '';
}
}
// Retrieve all records array indexed (multiple Records)
Function getRowsArray ($ SQL ){
$ This-> mysql_query_rst ($ SQL );
If (mysql_errno () = 0 ){
While ($ row = mysql_fetch_array ($ this-> result, MYSQL_ASSOC )){
$ This-> rowsArray [] = $ row;
}
Return $ this-> rowsArray;
} Else {
Return '';
}
}
// Update, delete, and add records. The number of affected rows is returned.
Function uidRst ($ SQL ){
If ($ this-> conn = ''){
$ This-> init_conn ();
}
@ Mysql_query ($ SQL );
$ This-> rowsNum = @ mysql_affected_rows ();
If (mysql_errno () = 0 ){
Return $ this-> rowsNum;
} Else {
Return '';
}
}
// Obtain the corresponding field value, a digital index, and mysql_array_rows is a field Index
Function getFields ($ SQL, $ fields ){
$ This-> mysql_query_rst ($ SQL );
If (mysql_errno () = 0 ){
If (mysql_num_rows ($ this-> result)> 0 ){
$ Tmprows = @ mysql_fetch_row ($ this-> result );
$ This-> fields = $ tmplogs [$ fields];
}
Return $ this-> fields;
} Else {
Return '';
}
}
// Error message
Function msg_error (){
If (mysql_errno ()! = 0 ){
$ This-> msg = mysql_error ();
}
Return $ this-> msg;
}
// Release the result set
Function close_rst (){
Mysql_free_result ($ this-> result );
$ This-> msg = '';
$ This-> fieldsNum = 0;
$ This-> rowsNum = 0;
$ This-> filesArray = '';
$ This-> rowsArray = '';
$ This-> idsubtitle = '';
$ This-> idusername = '';
}
// Close the database
Function close_conn (){
$ This-> close_rst ();
Mysql_close ($ this-> conn );
$ This-> conn = '';
}
}
?>
Use of Class 21 instances and md5 encryption of passwords
The Code is as follows:
$ Conne = new opmysql ();
$ Conne-> getRowsArray ($ SQL );
$ Conne-> close_conn ();
$ Password = "123456 may 1234 ";
Echo md5 ($ password. "); // The output is a 32-bit ciphertext, and there is no decryption function. The Hong Kong virtual host can implement simple encryption.
?>
, Website Space