SELECT statement is used to select data from the database

Source: Internet
Author: User
Keywords Network programming Mysql tutorial
Tags data echo example function mysql mysql tutorial mysql_fetch_array mysql_query
Select the data from the database table

SELECT statement is used to select data from the database.

Syntax SELECT column_name (s) FROM table_name

Note: SQL statements are case insensitive. SELECT and select equivalent.

In order for PHP to execute the above statement, we must use the mysql_query () function. This function is used to send queries or commands to MySQL.

example

The following example picks all the data stored in the "Person" table (the * character selects all the data in the table):

<? php $ con = mysql_connect ("localhost", "peter", "abc123"); if ($ con) {die ('Could not connect:'. mysql_error ());} mysql_select_db ("my_db", $ ; $ result = mysql_query ("SELECT * FROM person"); while ($ row = mysql_fetch_array ($ result)) {echo $ row ['FirstName']. "". $ row ['LastName']; echo " <br /> ";} mysql_close ($ con);?>

The above example stores the data returned by the mysql_query () function in the $ result variable. Next, we use the mysql_fetch_array () function to return the first row from the recordset as an array. Each subsequent call to the mysql_fetch_array () function will return the next row in the recordset. The while loop statement loops through all the records in the recordset. To output the value of each row, we use PHP's $ row variables ($ row ['FirstName'] and $ row ['LastName']).

Output of the above code:

Peter Griffin Glenn Quagmire Show results in an HTML table

The following example selects the same data as above, but will display the data in an HTML form:

<? php $ con = mysql_connect ("localhost", "peter", "abc123"); if ($ con) {die ('Could not connect:'. mysql_error ());} mysql_select_db ("my_db", $ $ result = mysql_query ("SELECT * FROM person"); echo "<table border = '1'> <tr> <th> Firstname </ th> <lastname </ th> </ tr> ; while ($ row = mysql_fetch_array ($ result)) {echo "<tr>"; echo "<td>". $ row ['FirstName']. "</ td>"; echo "<td>". $ row ['LastName']. "</ td>"; echo "</ tr>";} echo "</ table>"; mysql_close ($ con);?>

Output of the above code:

Firstname Lastname Glenn Quagmire Peter Griffin
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.