简体   繁体   中英

PHP Echo not displaying data from MySql

Just working on this little project, and need some help display data in the table. Tried troubleshooting but am unable to determine why the echo is not displaying data in the table.

It's displaying this instead in the source code:

<td><?echo $row['City'];?> </td>
<td><?echo $row['Population'];?> </td>
<td><?echo $row['Country'];?> </td>
<td><?echo $row['Continent'];?> </td>
<td><?echo $row['Region'];?> </td>

The result table displayed in the bowser: http://postimg.org/image/sakgymgjv/

Any answer to this is very much appreciated...

Thanks

<?  
mysql_connect("localhost", "....", "...") or die(mysql_error()); 
mysql_select_db("world") or die(mysql_error()); 

$query = "Select a.Name as City, a.Population, b.Name As Country, b.Continent, b.Region from city a LEFT JOIN country b ON a.CountryCode=b.Code";    
$result = mysql_query($query);

?>
<table summary="Cities of the world.">
<thead>
    <tr>
        <th scope="col">City</th>
        <th scope="col">Population</th>
        <th scope="col">Country</th>
        <th scope="col">Continent</th>
        <th scope="col">Region</th>
    </tr>
</thead>
<tbody>
<tr>
<?
while ($row = mysql_fetch_array($result,MYSQL_ASSOC);)
{   
?>  <td><?echo $row['City'];?></td>
    <td><?echo $row['Population'];?></td>
    <td><?echo $row['Country'];?></td>
    <td><?echo $row['Continent'];?></td>
    <td><?echo $row['Region'];?></td>
<?
}
mysql_close();
?>
</tr>
</tbody>
</table>

Your syntax is incorrect, unless you have shorttags enabled:

<td><?echo $row['City'];?> </td> should be <td><?php echo $row['City'];?> </td>

Notice the missing php from your code. To elaborate, this is what your code should look like:

<?php  
mysql_connect("localhost", "....", "...") or die(mysql_error()); 
mysql_select_db("world") or die(mysql_error()); 

$query = "Select a.Name as City, a.Population, b.Name As Country, b.Continent, b.Region from city a LEFT JOIN country b ON a.CountryCode=b.Code";    
$result = mysql_query($query);

?>

and

<?php
while ($row = mysql_fetch_assoc($result))
{   
?>  <td><?php echo $row['City']; ?></td>
    <td><?php echo $row['Population']; ?></td>
    <td><?php echo $row['Country']; ?></td>
    <td><?php echo $row['Continent']; ?></td>
    <td><?php echo $row['Region']; ?></td>
<?php
}
mysql_close();
?>

删除此行中的半逗号

while ($row = mysql_fetch_array($result,MYSQL_ASSOC))

It shouldn't make a difference, but try taking the ; out of the while loop here:

while ($row = mysql_fetch_array($result,MYSQL_ASSOC);)

Otherwise check the logs for php and mysql. Make sure there's not an error. Also, I'm assuming the database is populated....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM