2013-03-07 113 views
0

嘿工作在一個小項目,但我似乎有我的代碼的特定部分的問題。問題與數組空白回聲

第一部分精美地工作,並在產品類別中顯示產品。 http://mkiddr.com/phptests/shopping/category.php?id=2

但是,問題似乎是要顯示的類別描述,它是從一個單獨的查詢派生到它自己的數組中。我已經使用了

echo(mysqli_num_rows($result2)); 

這似乎是從查詢中計算正確的行數,表明SQL正在完美工作。

我很感謝這方面的幫助我是一個完整的需求,我已經修補並編輯了這個代碼以滿足我的需求(由大學提供)。 P.S我知道有安全漏洞。

<?php 
session_start(); 
include "conn.php"; 
include "header.php"; 

if (isset($_GET['id'])){ 
$CategoryID = $_GET['id']; 
$q="SELECT ProductID, ProductName FROM Products WHERE CategoryID=$CategoryID"; 
$d="SELECT `Desc` FROM ProductCategories WHERE CategoryID=$CategoryID"; 

$result = mysqli_query($_SESSION['conn'],$q); 
$result2 = mysqli_query($_SESSION['conn'],$d) or die(mysql_error()); 

echo "<div>"; 
while ($row = mysqli_fetch_row($result)){ 
    echo "<p><a href='product.php?id=".$row[0]."'>".$row[1]."</a></p>"; 
} 
echo "</div>"; 
mysqli_free_result($result); 

//Description 
echo(mysqli_num_rows($result2)); //Test SQL 

echo "<div>"; 
while ($myResult = mysqli_fetch_assoc($result2)){ 
    echo "<p>".$myResult[0]."</p>"; 
} 
echo "</div>"; 
} 
include "footer.php"; 
?> 

回答

4

你取錯:

while ($myResult = mysqli_fetch_assoc($result2)){ 
           ^^^^^--- produces a non-numerically keyed array 

你可能想

echo $myResult['name_of_field'] 

mysqli_fetch_row($result2) 
      ^^^--returns a numerically keyed array. 

代替。

+0

太棒了!你已經解決了,非常感謝。 – 2013-03-07 18:49:13