PHP Echo不显示来自MySql的数据

时间:2014-08-12 04:04:48

标签: php mysql html-table

正在处理这个小项目,需要一些帮助才能在表格中显示数据。 尝试排除故障,但无法确定回声未在表格中显示数据的原因。

它在源代码中显示了这个:

<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>

结果表显示在bowser中:http://postimg.org/image/sakgymgjv/

非常感谢任何对此的回答......

由于

<?  
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>

3 个答案:

答案 0 :(得分:3)

您的语法不正确,除非您启用了短标记:

<td><?echo $row['City'];?> </td>应为<td><?php echo $row['City'];?> </td>

请注意代码中缺少的php。详细说明,这就是您的代码应该是这样的:

<?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);

?>

<?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();
?>

答案 1 :(得分:0)

删除此行中的半逗号

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

答案 2 :(得分:-1)

它不应该有所作为,但尝试采取;在这里的while循环:

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

否则检查日志中的php和mysql。确保没有错误。另外,我假设数据库已填充....