SQL Select Query Tutorial

Source: Internet
Author: User
Tags php and mysql php code php script

You have seen two types of MySQL-so-far queries: queries we used to build tables and queries, we used to insert data into our newly created table. The query in this lesson is the selection that is used to get information from the database, so that its data can be used in our PHP script.


Information retrieval from MySQL
Finally, we can use these data to build a dynamic PHP page in our MySQL database. In this example, we will select all of our table "examples" and make it a nicely formatted HTML table. Keep in mind that if you do not understand HTML or PHP code, be sure to check out the HTML and/or PHP tutorials (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.
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>";
}

Age
Name
Timmy Mellowman 23
Sandy Smith 21st
Bobby Wallace 15

Since we have only three items in our table, three rows appear above. If you add more entries to your database table, then you will see each increment appearing consecutively in the above table. If you don't understand the PHP above, you can view our PHP array tutorials and PHP loop tutorials.

' $ result = mysql_query ... '
When you select items from the database using mysql_query, the data is returned as a result of MySQL. Now that we're going to use this data on our table, we need to store it in a variable. $ results now have the results from US mysql_query.

SELECT * www.111cn.net/database/database.html
In English, this line of code content "selects everything from the table example." "The asterisk is held outside the card in MySQL just tell MySQL to retrieve every field from the table."

' and ($ row = mysql_fetch_array ($ result)
The Mysql_fetch_array feature gets the next generation of online associative arrays from a MySQL result. By putting it in a while loop it will continue to fetch the next array until no future array is fetched. This feature can be called multiple times, but will return false when the last associative array has been returned.

By putting this function within the scope of conditional statements, and rings, we can kill two birds a stone.

We can retrieve the next associative array from our MySQL resources, $ results so that we can print out the names and age of the people.
We can tell the while loop to stop PRINTINGN information when the MySQL resource goes back to the past array, as the false return arrives at the end, which causes the while loop to stop.
In our MySQL table "Example", there are only two areas we care about: name and age. The key to these areas is to extract the data from our associative array. In order to let us use the name $ row [' name '], let our age use $ row [' age '].

Practice what you have learned
Using the query, we've provided or made one of the new attempts and turned it into a formatted HTML table. This may be helpful in trying other methods of HTML format. See which one you like best!

By now you should start to understand how powerful PHP and MySQL are when used together. The task that you can complete with MySQL and PHP will be almost impossible to do on the one hand in HTML. Imagine trying to build an HTML table 6000 without using the MySQL database and PHP while loop!

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.