2012-08-22 39 views
0

我已經廣泛查看網站,並得出結論,我遇到了很多人做的問題,但沒有我見過的答案似乎工作!另一個.... HTML表不填​​充與MySQL/PHP

基本上,我試圖從存儲在mySQL表中的數據填充HTML表。數據位於名爲「類別」的表格中。當我加載頁面時,表頭會出現,但沒有表格數據。

我已經寫了並重寫了我的代碼不少於4次 - 它可以很好地作爲「ul」使用,或者只是作爲純文本呈現時,但只要我將它放入「表」中似乎停止工作!

這裏是我的代碼:

<?php 
include("includes/dbconnect.php"); //This works fine as tested on the page 

$sql = "SELECT * FROM products"; 
$myData = mysql_query($sql) or die (mysql_error()); 
$product = mysql_fetch_array($myData); 

?> 

<html> 
<head> 
<title></title> 
</head> 
<body> 
    //This plain text works 
<?php do { 
echo $product['title']." <br />"; 
} while ($product = mysql_fetch_array($myData)); 
//----------------------------------------- 
    //Table - doesnt work 
echo "<table border=1> 
<tr> 
    <th>Title</th> 
    <th>Category</th> 
    <th>Sport</th> 
    <th>Team</th> 
    <th>Price</th> 
    <th>Shipping</th> 
</tr>"; 
while ($product = mysql_fetch_array($myData)) { 
    echo "<tr>"; 
    echo "<td>".$product['title']."</td>"; 
    echo "<td>" . $product['category'] . "</td>"; 
    echo "<td>" . $product['sport'] . "</td>"; 
    echo "<td>" . $product['team'] . "</td>"; 
    echo "<td>" . $product['price'] . "</td>"; 
    echo "<td>" . $product['shipping'] . "</td>"; 
    echo "</tr>"; 
} 
echo "</table>"; 
?> 


</body> 
</html> 

我真的在我束手無策,香港專業教育學院的工作我的方式,通過無數次的YouTube教程和仍然出於某種原因的代碼不會工作。誰能幫忙?

+0

print_r($ product)while循環開始後,它顯示你所期望的? – 2012-08-22 06:26:58

+0

如果您已經獲取了數據,則不能再這樣做! –

+1

請不要將'mysql_ *'函數用於新代碼。他們不再被維護,社區已經開始[棄用流程](http://news.php.net/php.internals/53799)。看到[**紅框**](http://php.net/mysql-connect)?相反,您應該瞭解[準備好的陳述](http://en.wikipedia.org/wiki/Prepared_statement)並使用[PDO](http://php.net/pdo)或[MySQLi](http:// php.net/mysqli)。如果你不能決定,試試[這篇文章](http://php.net/mysqlinfo.api.choosing)。如果你關心學習,[這裏是很好的PDO教程](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers)。 –

回答

0

你的代碼中包含了很多bug和實施

試試這個,如果你的作品:

<?php 
include("includes/dbconnect.php"); 
$sql = "SELECT * FROM products"; 
$myData = mysql_query($sql) or die (mysql_error()); 
?> 
<html> 
<head> 
<title></title> 
</head> 
<body> 
    //This plain text works 
<?php 

if(mysql_num_rows ($myData) > 0) 
{ 
    $html = "<table border=1><tr> 
        <th>Title</th> 
        <th>Category</th> 
        <th>Sport</th> 
        <th>Team</th> 
        <th>Price</th> 
        <th>Shipping</th></tr>"; 

    while ($product = mysql_fetch_array($myData)) 
    { 
     $html .= "<tr>"; 
     $html .= "<td>".$product['title']."</td>"; 
     $html .= "<td>".$product['category']."</td>"; 
     $html .= "<td>".$product['sport']."</td>"; 
     $html .= "<td>".$product['team']."</td>"; 
     $html .= "<td>" . $product['shipping'] . "</td>"; 
     $html .= "</tr>"; 
    } 
    $html .= "</table>"; 
    echo $html; 
} 
else 
{ 
    echo "No Product Found"; 
} 
?> 
</body> 
</html> 

如果這對你的作品,你可以計算出問題或我會解釋:)

+0

偉大的答案 - 簡單易用!即使我明白了!真的很感謝幫助! – Anthony

1

您已經在代碼頂部的do while循環中運行了mysql_fetch_array。您無法多次拉出結果行。相反,推動所有返回的行到一個數組:

$products = array(); 
while ($product = mysql_fetch_array($myData)) { 
    $products[] = $product; 
} 

然後,您可以遍歷$products多次你想構建自己的頁面。因此,對於你的表,這將是這樣的:

echo "<table border=1> 
<tr> 
    <th>Title</th> 
    <th>Category</th> 
    <th>Sport</th> 
    <th>Team</th> 
    <th>Price</th> 
    <th>Shipping</th> 
</tr>"; 
foreach($products as $item) { 

    echo "<tr>"; 
    echo "<td>" . $item['title'] . "</td>"; 
    echo "<td>" . $item['category'] . "</td>"; 
    echo "<td>" . $item['sport'] . "</td>"; 
    echo "<td>" . $item['team'] . "</td>"; 
    echo "<td>" . $item['price'] . "</td>"; 
    echo "<td>" . $item['shipping'] . "</td>"; 
    echo "</tr>";  

} 
echo "</table>"; 
+1

感謝您的回覆!我不知道在哪裏放置第一塊php(無論是否在'do while'循環中,但是再次感謝您重新編寫代碼! – Anthony

0

如果代碼真的看起來像這樣除去do-while循環,一切都在那裏你遍歷你$mydata因爲你浪費內部MySQL的櫃檯,將工作

在第二循環中的$result已經在最後的意思,任你再查詢或你推陣列中的所有結果多次使用

+0

我不知道如何將結果推送到數組中 - 但有人在您的帖子後回答 - 感謝您的幫助! – Anthony

+0

one liner;) $ array [] = $ p; – Soundz

0

您已經使用這個拉你的數據一次.... $product = mysql_fetch_array($myData); 所以只需要使用這個數組ACH循環....

foreach($product as $sProduct){ 
// User $sProduct to access that single product. 
}