0% found this document useful (0 votes)
18 views1 page

Display The Result in An HTML Table

The PHP code connects to a MySQL database, selects data from a table, and displays it. It connects to the database, selects the "my_db" database, queries the "Persons" table, stores the results in the $result variable. It then loops through the results, fetching each row as an array and displaying the FirstName and LastName values. It closes the database connection at the end. A second example displays the same data in an HTML table instead of plain text.

Uploaded by

Harold B. Lacaba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views1 page

Display The Result in An HTML Table

The PHP code connects to a MySQL database, selects data from a table, and displays it. It connects to the database, selects the "my_db" database, queries the "Persons" table, stores the results in the $result variable. It then loops through the results, fetching each row as an array and displaying the FirstName and LastName values. It closes the database connection at the end. A second example displays the same data in an HTML table instead of plain text.

Uploaded by

Harold B. Lacaba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

<?

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

The example above 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 call to mysql_fetch_array() returns the next row in the recordset. The while loop loops through all
the records in the recordset. To print the value of each row, we use the PHP $row variable
($row['FirstName'] and $row['LastName']).
The output of the code above will be:
Peter Griffin
Glenn Quagmire

Display the Result in an HTML Table


The following example selects the same data as the example above, but will display the data in an
HTML table:
<?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 "<table border='1'>
<tr>
<th>Firstname</th>
<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);
?>

You might also like