In the MySQL Select query tutorial, select is used by every WEB developer or program developer to perform the simplest database query operation and return the information you want.
You have seen two types of MySQL queries so far: queries are used to create tables and queries, and the data we use is inserted into the newly created table. In this case, the query is used to obtain information from the database. Therefore, the data can be used in our PHP script.
Information Retrieval from MySQL
Finally, we can use this data to create a dynamic PHP page in our MySQL database. In this example, We will select "example" in all our tables and make it a well-formatted HTML table. Remember, if you do not understand HTML or PHP code, check the HTML and/or PHP tutorial (Sunday ).
$ Result = mysql_query ("SELECT * FROM example ")
Or die (mysql_error ());
Echo "<table border = '1'> ";
Echo "<tr> <th> Name </th> <th> Age </th> </tr> ";
// Keeps getting the next row until there are no more to get
While ($ row = mysql_fetch_array ($ result )){
// Print out the contents of each row into a table
Echo "<tr> <td> ";
Echo $ row ['name'];
Echo "</td> <td> ";
Echo $ row ['age'];
Echo "</td> </tr> ";
}
| Name |
Age |
| Tiancmellowman |
23 |
| Sandy Smith |
21 |
| Bobby Wallace |
15 |
Because we only have three projects on our table and three rows above. If you add more entries to your database table, you will see that each addition appears continuously in the preceding table. If you do not understand the above PHP, you can view our PHP array tutorial & PHP loop tutorial.
'$ Result = mysql_query ...'
When you select the project to use mysql_query from the database, the data is returned as a MySQL result. Since we want to use this data on our table, we need to store it in variables. $ The results currently have results from mysql_query.
Select * www.111cn.cn/database/database.html
In English, the Code content of this row is "select all slave table Examples. The asterisk (asterisk) is used to retrieve each field from the table as MySQL just told.
'And ($ ROW = mysql_fetch_array ($ result)
The mysql_fetch_array function obtains the results of the next generation of Online Association arrays from a MySQL. By placing it in a while loop, we will continue to retrieve the next array until there is no future array capture. This function can be called multiple requests, but returns FALSE when the last joined array is returned.
By moving conditional statements within this function, we can kill two birds and one stone.
We can retrieve the next associated array from our MySQL resource, $ result, so that we can print the name and age of the person.
We can tell the while loop to stop the printingn information, MySQL resources return to the past array, as a false return to the end, this will cause the while loop to stop.
There are only two fields in our MySQL table "example": name and age. The key to these fields is to extract data, our associated array. In order to let us use the name $ row ['name'], let our age use $ row ['age'].
What are your lessons
Using query, we have provided or made one of the new attempts and converted it into a formatted HTML table. This may be helpful to try other methods in HTML format. Which one do you like the most!
Now you should begin to understand how powerful PHP and MySQL are used together. The task, you can completely work with MySQL and PHP will be almost impossible to do this on the one hand in HTML. Imagine creating an HTML table with 6000 items without using the while loop of MySQL database and PHP!