PHP MySQL Getting Started tutorial read data from a database _php tutorial

Source: Internet
Author: User
A super simple PHP MySQL introductory tutorial to read data from the database instance, you can refer to.

The SELECT statement is used to select data from the database.

Selecting data from a database table

The SELECT statement is used to select data from the database.

Grammar


SELECT column_name (s) from table_name
Note: The SQL statement is not case sensitive. Select is equivalent to select.

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

Example

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

copy code

"!--? php
$con = mysql_connect ("Local Host "," Peter "," abc123 ");
if (! $con)
{
Die (' Could not connect: '. Mysql_error ());
}
mysql_select_db ("my_db", $con);
$result = mysql_query ("SELECT * from Persons");
while ($row = Mysql_fetch_array ($result))
{
echo $row [' FirstName ']. " " . $row [' LastName '];
echo "
";
}
Mysql_close ($con);
?

The above example stores the data returned by the mysql_query () function in a $result variable. Next, we use the mysql_fetch_array () function to return the first row from the recordset in the form of 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. In order to output the value of each line, we used PHP's $row variable ($row [' FirstName '] and $row [' LastName '].

The output of the above code:


Peter Griffin
Glenn Quagmire
--------------------------------------------------------------------------------


Displaying results in an HTML table

The following example selects the same data as the example above, but displays the data in an HTML table:

code as follows copy code

"!--? php
$con = mysql_connect (" LocalHost "," Peter "," abc123 ");
if (! $con)
{
Die (' Could not connect: '. Mysql_error ());
}
mysql_select_db ("my_db", $con);
$result = mysql_query ("SELECT * from Persons");
Echo











, while ($row = Mysql_fetch_array ($result)) {echo " "; echo " "; echo " "; echo " "; }echo "
Firstname Lastname
". $row [' FirstName ']. "". $row [' LastName ']. "
";
Mysql_close ($con);
?

The output of the above code:


Firstname Lastname
Glenn Quagmire
Peter Griffin

http://www.bkjia.com/PHPjc/630704.html www.bkjia.com true http://www.bkjia.com/PHPjc/630704.html techarticle a super simple PHP MySQL introductory tutorial to read data from the database instance, you can refer to. The SELECT statement is used to select data from the database. Select data from database table SEL ...

  • 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.