Home > Backend Development > PHP Tutorial > PHP MySQL introductory tutorial: Reading data from the database_PHP tutorial

PHP MySQL introductory tutorial: Reading data from the database_PHP tutorial

WBOY
Release: 2016-07-13 17:06:26
Original
1003 people have browsed it

A super simple introductory tutorial for PHP MySQL with an example of reading data from the database for your reference.

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

Select data from database table

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

Grammar


SELECT column_name(s) FROM table_name
Note: SQL statements are 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 queries or commands to MySQL.

Example

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

Output of the above code:
The code is as follows
 代码如下 复制代码

$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");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "
";
 }
mysql_close($con);
?>
Copy code

$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");
while($row = mysql_fetch_array($result))

{

echo $row['FirstName'] . " " . $row['LastName'];

echo "
";

 代码如下 复制代码

$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);
?>
}

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 returns the next row in the recordset. The while loop statement loops through all records in the recordset. To output the value of each row, we use PHP's $row variables ($row['FirstName'] and $row['LastName']).

Peter Griffin

Glenn Quagmire Display results in HTML table The following example selects the same data as the above example, but displays the data in an HTML table:
The code is as follows Copy code
$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); ?> Output of the above code: Firstname Lastname Glenn Quagmire Peter Griffin http://www.bkjia.com/PHPjc/630704.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630704.htmlTechArticleA super simple PHP MySQL introductory tutorial on reading data from the database for your reference. The SELECT statement is used to select data from the database. Select data from database table SEL...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template