Getting started with PHPMySQL: reading data from a database

Source: Internet
Author: User
This is a super simple PHPMySQL getting started tutorial. you can refer to the SELECT statement to SELECT data from the database and SELECT data from the database table, the SELECT statement is used to SELECT data from the database. syntax: SEL... this is a simple PHP MySQL getting started tutorial. you can refer to the SELECT statement to SELECT data from the database and SELECT data from the database table, the 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 statements are equivalent to select statements.

To allow PHP to execute the preceding statement, we must use the mysql_query () function, which is used to send a query or command to MySQL.

For example, the following example selects all data stored in the "Persons" table (* selects all data in the table). The code is as follows:

 ";}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 record set in the form of an array, each subsequent call to the mysql_fetch_array () function will return the next row in the record set. the while loop statement will record all records in the set cyclically. to output the values of each row, we use the $ row variable of PHP ($ row ['firstname'] and $ row ['lastname']).

Output of the above code:

Peter Griffin

Glenn Quagmire

The result is displayed in an HTML table. The data selected in the following example is the same as that in the preceding example, but the data is displayed in an HTML table. the code is as follows:

   Firstname Lastname ";while ($row = mysql_fetch_array($result)) {    echo "";    echo "" . $row['FirstName'] . "";    echo "" . $row['LastName'] . "";    echo "";}echo "";mysql_close($con);

// Output of the above code:

Firstname  Lastname   Glenn  Quagmire   Peter  Griffin


Address:

Reprinted at will, but please attach the article address :-)

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.