I’m having difficulties to display data from mysql database to PHP generated table. Interesting that query shown below runs fine in SQL, but when I try to wrap it in PHP, it displays empty table without giving any errors.
SELECT city.name, cinema.name, whattime, whichdate
FROM city, cinema, relationship
WHERE cinema.city = city.id
AND cinemaid = cinema.id
ORDER BY whichdate
Here is the PHP code:
<?php
$usr = "admin";
$pwd = "";
$db = "dbname";
$host = "localhost";
$con = mysql_connect($host, $usr, $pwd) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
$sql = "select city.name, cinema.name, whichdate, whattime ";
$sql .= "from city, cinema, relationship ";
$sql .= "where cinema.city = city.id ";
$sql .= "and cinemaid = cinema.id ";
$sql .= "order by whichdate";
$query = mysql_query($sql, $con) or die(mysql_error());
echo "<table id='premiere'>";
echo "<tr> <th>CITY</th> <th>CINEMA</th> <th>DATE</th> <th>TIME</th></tr>";
while($result = mysql_fetch_array( $query )) {
echo "<tr><td>";
echo $result['city.name'];
echo "</td><td>";
echo $result['cinema.name'];
echo "</td><td>";
echo $result['relationship.whichdate'];
echo "</td><td>";
echo $result['relationship.whattime'];
echo "</td></tr>";
}
echo "</table>";
?>
Any help would be appreciated.
Your fetched array will not include the table names as array keys, only the column names (as in
$result['whichname'])Use column aliases for
city.nameandcinema.nameto differentiate them.Then, instead of:
use only the column names (or aliases) as array keys: