Mysql database operations (php Tutorial 4)

Source: Internet
Author: User
Tags dbase instance method php tutorial

<? Php Tutorial
Class opmysql tutorial {
Private $ host = 'localhost'; // server address
Private $ name = 'root'; // logon account
Private $ pwd = ''; // logon password
Private $ dbase = 'a0606123620 '; // database tutorial 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 = '';
}
}
?>

Instance method

 

The code is as follows: Copy code
<? Php
$ Conne = new opmysql ();
$ Conne-> getrowsarray ($ SQL );
$ Conne-> close_conn ();
$ Password = "123456 May 1234 ";
Echo md5 ($ password. "www.111cn.net"); // The output is a 32-bit ciphertext. There is no decryption function and simple encryption function can be implemented.
?>


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.

Related Article

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.