I am trying to extract data from a mysql database and list out two items from each record. Each record contains a name and an address among other fields. The trouble I’m having is when it comes to processing each array and then echoing each pair(name, address) for use in something simple like a list or a table. I think the answer is probably to process each pair into an array, then when it comes to echoing the results, process the array seperately.
Am I writing the sql query correctly?
What’s the proper way to write the php for fetching results and echoing it out in my html doc?
<?php
// 1. write the SQL query
$sql = “SELECT Name, Address FROM shops”;
// 2. Query the database
$result = mysql_query($sql, $connection);
[COLOR="Red"]// 3. Fetch the results
while($row = mysql_fetch_assoc($result))
{
$names[] = $row['Name'];
$addr[] = $row['Address'];
}[/COLOR]
include "includes/home.html.php";
?>
relevant code from the html include file below
ol>
<?php foreach ($names as $name); ?>
//do i need to create another similar statement here for $addr?
<li>
<?php echo htmlspecialchars($name, ENT_QUOTES, ‘UTF-8’); ?>
<?php endforeach; ?></li>
</ol>