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):
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 "
Firstname |
Lastname |
";
while($row = mysql_fetch_array($result))
{
echo "";
echo "" . $row['FirstName'] . " | ";
echo "" . $row['LastName'] . " | ";
echo " ";
}
echo " ";
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']). |
Output of the above code:
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 "
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
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...
|