PHP Collation (iv): MySQL

Source: Internet
Author: User

The main contents are as follows:

1. How to connect a database

2. How to operate the database

(1) How to execute SQL language

(2) How to process the returned result set

Method One: Process oriented (obsolete, just understand)

Assume:

1 $username =your_name;2 $userpass =your_pass;3 $dbhost =localhost;4 $dbdatabase =your_database;
1//Generate a Connection 2 $db _connect=mysql_connect ($dbhost, $username, $userpass) or die ("Unable to connect to the mysql!"); 3  4//Select a database to operate 5 mysql_select_db ($dbdatabase, $db _connect); 6  7//execute MySQL statement 8 $result =mysql_query ("Select Id,name from user "); 9 10//Extract Data $row =mysql_fetch_row ($result);

Description

① using the @ (Error control operator) before functions such as mysql_connect (), mysql_select_db (), can ignore the error message generated by the system, and then we use Die () to customize the error message;

② extract data when, in addition to the above mysql_fetch_row, common there are mysql_fetch_assoc and mysql_fetch_array, the specific differences please check PHP Manual;

③ for the return value of the mysql_query () function, if the executed statement has a return value (such as SELECT, SHOW, describe, and so on), the corresponding data is returned (on success) or FALSE (on failure), if the executed statement has no return value (such as Delete, DROP, INSERT, update, and so on), returns True (on success) or FALSE (on Failure).

Method Two: Object-oriented (required to connect MySQL database)

1//Create database object 2 $db =new mysqli ("localhost", "root", "123", "exam"); 3//judgment is not performed successfully 4!mysqli_connect_error () or Die ("Connection failed!"); 5//Execute SQL statement 6 $result = $db->query ("SELECT * from student"); 7//Processing result set 8 $arr = $result->fetch_row (); 9//Output Result: Var_dump ($arr);

Classic examples:

This is the example is embedded in the HTML, of course, not embedded in is also possible

1. Data in the database is output in tabular form

 1 <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 2 

Results preview:

  

2. Data from the database is presented in the form of a drop-down list

 1 <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 2 

Results:

This is the page effect

This is the form of code display

Summarize:

Summarize these two examples above:

(1) First of all, these two examples are used to control the output of PHP (carefully understand this sentence), that is, the content of the database to present a user, of course, here can add some style, to the user a good user experience. When designing, consider the data and HTML separately, and then find the interface between them. Merge the two together. PHP extracts data from the database and uses HTML to better present the data to the user. For example, the extracted data is generally an array, the user is not able to understand the array, to use the form of HTML table to display to the user, which is to consider and merge the two separately.

(2) Pay attention to details, whether it is the details of the user experience or the details of the programming. Details determine success or failure

3. Login with PHP and database

Implementation of the login is a step more to fill in the user information and database user information to do the comparison. (If the combination of JS and CSS here can achieve a good user experience effect)

Login interface:

    

 1 <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 2 

Results:

Login Processing page:

 1 <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 2 

Note: The verification of this place is verified with a matching number, the only match for 1, the login succeeds, of course, this is not the only way

4. The user interface to achieve data deletion and modification (change and check difficulties)

Total page:

 1 <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 2 

Effect:

Increase:

 1 <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 2 

1 <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 2 

Delete:

1 <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 2 

Modify:

5. Encapsulating the database connection (mainly the connection function)

Class of the encapsulated linked database:

1 <?php 2  3 class Dbda 4 {5 public     $host = "localhost";//server address 6 public     $uid = "root";//Database username 7     PU Blic $pwd = "123"; Password for database 8      9     //Execute SQL statement, function that returns the corresponding result     //$sql is the SQL statement to execute//     $type is the type of SQL statement, 0 for adding or deleting, 1 for querying the     //$ DB represents the database to be manipulated public     function Query ($sql, $type =1, $db = "Testa")     {         //Build Connection object         $conn = new mysqli ($this->host, $this->uid, $this->pwd, $db);         //Determine if the connection succeeded         !mysqli_connect_error () or Die ("Connection failed! ")         //Execute SQL statement         $result = $conn->query ($sql), and/         /Determine SQL statement type         ($type = =1)             (+//) If the query statement returns a two-dimensional array of result sets, return             $result->fetch_all ();         }30         else31         {32             //If it is a different statement, return TRUE or false33             return $result;         }35     }36     37}

Pagination class

View Code

Mainly how to use:

There are more important functions in this class as well as the main functions used

The constructors and fpage () functions of the class

    

PHP Collation (iv): MySQL

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.