[英]PHP Echo not displaying data from MySql
只是從事這個小項目,並需要一些幫助才能在表中顯示數據。 嘗試進行故障排除,但是無法確定為什么回顯未在表中顯示數據。
而是在源代碼中顯示它:
<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>
除非啟用了短標記,否則語法不正確:
<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();
?>
刪除此行中的半逗號
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
它不應該有所作為,但請嘗試; 在while循環之外:
while ($row = mysql_fetch_array($result,MYSQL_ASSOC);)
否則,請檢查php和mysql的日志。 確保沒有錯誤。 另外,我假設數據庫已填充。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.