2011-11-27 48 views
0

因此,在網上商店寫下我的第一個PHP網站。我已經寫了一段代碼,用於選擇產品數據庫和類別的所有產品,然後使用相同的product_id選擇前面的圖片顯示圖像。PHP錯誤:「提供的參數不是有效的MySQL結果資源」

代碼如下:

<?PHP 
include_once "db.php"; 

$result = "(select * FROM products WHERE product_type = 'weddingdressaline') inner join (select * FROM images WHERE postion = 'front') images on products.product_id = images.product_id"; 

while($row = mysql_fetch_array($result)) { 
    $content = $row['image']; 
    header('Content-type: image/jpg'); 
    echo $content; 
    echo $row['price']; 
} 

mysql_free_result($result); 
mysql_close(); 
?> 

但是當我運行該腳本,我得到了以下錯誤:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in **/weddingdressaline.php on line 4

Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in **/weddingdressaline.php on line 11

現在,我敢肯定,對於第二個錯誤的原因是因爲發生第一個錯誤。任何幫助你可以給我會非常感激。

+3

歡迎堆棧溢出!你沒有做任何錯誤檢查。你需要*在mysql_query()調用後執行該操作,如果發生錯誤,使用'mysql_error()'輸出錯誤。否則,如果查詢失敗,腳本就會中斷。如何做到這一點在[mysql_query()'](http://php.net/mysql_query)手冊或本[參考問題。](http://stackoverflow.com/questions/6198104/reference -what-a-perfect-code-sample-using-the-mysql-extension) –

+0

@Pekka:你看到一個'mysql_query'調用嗎? ;) – Ryan

+1

@mini現在你說出來了! :)(尼爾,有關錯誤檢查的建議仍然成立) –

回答

5

您不會將查詢發送到SQL數據庫 - 它只是一個字符串。你需要調用mysql_query就可以了,就像這樣:

$result = mysql_query("(select * FROM products WHERE product_type = 'weddingdressaline') inner join (select * FROM images WHERE postion = 'front') images on products.product_id = images.product_id"); 

由於佩卡指出,雖然,你應該檢查錯誤也是如此。

+0

哈!發現得好。 –

+0

爲你的幫助而歡呼,我現在得到它的工作。我剝離它的基本知識,使其工作,但現在已經投入了錯誤檢查。但歡呼你所有的幫助。 –

1

您錯過了mysql_query。該$result行應閱讀以下內容:

$result = mysql_query($your_query_here); 
1

你是不是查詢數據庫...

$result = mysql_query("(select * FROM products WHERE product_type = 'weddingdressaline') inner join (select * FROM images WHERE postion = 'front') images on products.product_id = images.product_id"); 
while ($row = mysql_fetch_array($result)) 
{ 
$content = $row['image']; 
header('Content-type: image/jpg'); 
echo $content; 
echo $row['price']; 
} 
+0

這仍然是一個不好的例子,因爲它不檢查錯誤。只是說' –

+0

@Pekka:是的,它不會轉義輸出(被授予,它是一個JPEG),並且查詢仍然以可怕的方式寫入;) – knittl

1

你忘了使用的mysql_query()

$result = mysql_query("(select * FROM products WHERE product_type = 'weddingdressaline') inner join (select * FROM images WHERE postion = 'front') images on products.product_id = images.product_id"); 
相關問題