MySQL结果没有显示在表格中?

时间:2011-03-13 22:53:56

标签: php mysql database coda

我正在教自己PHP和MySQL,我正在尝试从我的数据库中检索一些信息并将其放入表中。

到目前为止,只显示了表列标题,并且没有为每列显示任何信息。它还需要很长时间才能显示PHP文件。

请指出我的代码存在的问题。

<?php
mysql_connect("localhost",$username,$password);
mysql_select_db($dbname) or die("Unable to select Database");
$query = "SELECT * FROM table_1";
$result = mysql_query($query);
$numcount = mysql_num_rows($result);
echo "<h2>$numcount rows in table_1.</h2>";
mysql_close();
?>

<table border="0" cellspacing="4" cellpadding="2">
<tr>
<th><font face="Futura">Type |</font></th>
<th><font face="Futura">Name |</font></th>
<th><font face="Futura">Street |</font></th>
<th><font face="Futura">Address1 |</font></th>
<th><font face="Futura">Address2 |</font></th>
<th><font face="Futura">Town |</font></th>
<th><font face="Futura">County |</font></th>
<th><font face="Futura">Postcode |</font></th>
<th><font face="Futura">Number |</font></th>
<th><font face="Futura">Latitude,Longitude</font></th>
</tr>

<?php
$i=0;
while ($i < 843) {
$type = mysql_result($result,$i,"type");
$name = mysql_result($result,$i,"name");
$street = mysql_result($result,$i,"street");
$addr1 = mysql_result($result,$im,"address1");
$addr2 = mysql_result($result,$im,"address2");
$town = mysql_result($result,$im,"town");
$county = mysql_result($result,$im,"county");
$postcode = mysql_result($result,$im,"postcode");
$number = mysql_result($result,$im,"number");
$latlong = mysql_result($result,$im,"latlong");
}
?>

<tr>
<td><font face="Futura"><?php echo $type;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $name;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $street;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $addr1;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $addr2;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $town;?></font></td>
</tr>
<?php
$i++;
?>
<?
echo "</table>"; 
?>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

  1. mysql_result()在你之前做mysql_close()时有效吗?

  2. 为什么不在这里使用mysql_fetch_row()?

  3. 为@XcodeDev编辑更多信息:

    当您需要单个结果时,可以使用mysql_result(),例如:

    $query = mysql_query("SELECT COUNT(id) FROM users");
    

    然后

    $count = mysql_result($query, 0);
    

    如果您希望使用多个数据生成一行结果,请使用

    $result = mysql_fetch_row($query); => $result[0], $result[1], $result[2] etc
    

    或者

    $result = mysql_fetch_assoc($query); => $result['type'], $result['name'] etc
    

    如果您希望使用多个数据生成几行结果,请使用

    while ($result = mysql_fetch_row($query)) {
        => $result[0], $result[1], $result[2] etc
    }
    

    while ($result = mysql_fetch_assoc($query)) {
        => $result['type'], $result['name'] etc
    }
    

答案 1 :(得分:0)

对于启动器,你有一个无限循环,因为$ i没有在循环内增加。如果你增加$ i,它应该解决问题。什么是$ im?

代码应如下所示:

<?php
$i=0;
while ($i < 843) {
    $type = mysql_result($result,$i,"type");
    $name = mysql_result($result,$i,"name");
    $street = mysql_result($result,$i,"street");
    $addr1 = mysql_result($result,$i,"address1");
    $addr2 = mysql_result($result,$i,"address2");
    $town = mysql_result($result,$i,"town");
    $county = mysql_result($result,$i,"county");
    $postcode = mysql_result($result,$i,"postcode");
    $number = mysql_result($result,$i,"number");
    $latlong = mysql_result($result,$i,"latlong");

?>

<tr>
<td><font face="Futura"><?php echo $type;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $name;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $street;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $addr1;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $addr2;?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $town;?></font></td>
</tr>
<?php
    $i++;
} // this is where the loop is ending.
?>
<?
mysql_close(); // close mysql connection after reading data
echo "</table>"; 
?>