2011-03-13 103 views
1

我在教自己的PHP和MySQL,我試圖從我的數據庫中檢索一些信息並將其放入表中。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> 

回答

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>"; 
?> 
+0

$ IM剛剛被實現爲一個拼寫錯誤, $ i在我提供的代碼片段底部附近遞增。 – 2011-03-13 23:01:40

+0

但是$ i在while循環內沒有增加 – 2011-03-13 23:03:26

+0

請注意,您正在循環外增加$ i。使用上面的代碼來修復它。 – Rasika 2011-03-13 23:04:01

2
  1. mysql_result()的作品,當你做mysql_close()之前?

  2. 爲什麼不在這裏使用mysql_fetch_row()?

隨着對@XcodeDev更多INFOS編輯:

當你需要一個單一的結果,例如可以使用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

@XcodeDev你應該習慣這種用法。 mysql_fetch_row或我的選擇是mysql_fetch_array總是更好。沒有必要定義或計算$ i。 – Kemal 2011-03-13 23:08:54

+0

@Ahmet凱末爾我對PHP/MySQL是全新的我只是按照教程。我現在會研究它。 – 2011-03-13 23:12:36