Fastest understanding of PHP programming Lecture 6: Mysql database operations

Source: Internet
Author: User
Tags dbase php database
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, it 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. "www.kuphp.com"); // The output is a 32-bit ciphertext. there is no decryption function and simple encryption function can be implemented.
?>

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.