2011-11-27 46 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"); 
相关问题