2013-03-07 71 views
0

在一個小型大學項目上開發一個小型php站點。似乎有幾個問題。當談到任何類型的編程時,我是一個完整的新人。我正在切斷和粘貼(從提供的腳本中),並試圖以我需要的方式將事物聚集在一起。PHP,MySQL查詢不起作用

我似乎有一個問題,從數據庫查詢收集數據,我已經有一個完美的腳本工作。我想添加另一個,但似乎無法得到這個工作。

http://mkiddr.com/phptests/shopping/category.php?id=2

目前category.php正在顯示該類別中的所有產品。不過,我希望它也顯示該類別的描述,你會在下面的代碼怎麼看我試着這樣做:

<?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); 

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

希望得到一些幫助!

更新的代碼(仍然沒有工作)

<?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); 

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 "<div>"; 
while ($result2 = mysqli_fetch_assoc($result2)){ 
    echo "<p>".$result2[0]."</p>"; 
    } 

    } 
    include "footer.php"; 
    ?> 
+0

檢查我的文章中的代碼。 – 2013-03-07 17:38:08

回答

4

一個我見過的錯誤是使用保留關鍵字。

$d未執行的原因是因爲列DESC是來自MYSQL的保留關鍵字。你可以用反引號delimite,或放在桌子上提供一個別名,使其工作,如

$d="SELECT `Desc` FROM ProductCategories WHERE CategoryID=$CategoryID"; 

OR

$d="SELECT a.Desc FROM ProductCategories a WHERE CategoryID=$CategoryID"; 

一點題外話,查詢如果變量的值(s)來自外部,則容易受到SQL Injection的影響。請看下面的文章,瞭解如何防止它。通過使用PreparedStatements你可以擺脫使用單引號圍繞值。

+0

啊,太棒了,這將是有道理的。 但是我似乎接收到以下錯誤:可捕捉的致命錯誤:類mysqli_result的對象無法轉換爲第19行上的/volume1/web/phptests/shopping/category.php中的字符串 – 2013-03-07 16:41:32

+0

第19行是什麼? – Barmar 2013-03-07 16:55:06

+0

echo「

」。$ result2。「

」; – 2013-03-07 16:58:32

1

首先記得要查詢獲取到一個數組:

$result2 = mysqli_fetch_assoc($result2); 

由於查詢被保存到一個關聯數組,你應該把它以這種方式 $result2['Desc']

<?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 = mysql_fetch_assoc(mysqli_query($_SESSION['conn'],$d)); 

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 "<div>"; 
      echo "<p>".$result2['Desc']."</p>"; 
echo "</div>"; 

    } 
    include "footer.php"; 
    ?> 
+1

這僅僅是錯誤的,'mysqli_fetch_row'不返回一個二維數組。 – Barmar 2013-03-07 16:53:53

+0

這是真的,我的壞。我用assoc命令編輯了我的答案。 – 2013-03-07 17:01:31

+0

現在你的答案不再是錯誤的,但它仍然不是一個解決方案。 'mysqli_fetch_array()'在他使用它的方式上工作的很好。 – Barmar 2013-03-07 17:05:00