0

So writing my first PHP website with a web store. I have written a piece of code that is suppose to select the products database with all the products of a category, and then display an image using the same product_id selecting the front picture.

Code is as follows:

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

But when I run the script I get the following errors:

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

Now I'm pretty sure that the reason for the second error is because the first error is occuring. Any help you could give me would be really appreciated.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Neil Grimmer
  • 1
  • 1
  • 1
  • 3
    Welcome to Stack Overflow! You are not doing any error checking. You *need* to do that after a `mysql_query()` call, and if an error occurs, output the error using `mysql_error()`. Otherwise, your script will just break if the query fails. How to do this is outlined in the [manual on `mysql_query()`](http://php.net/mysql_query) or in this [reference question.](http://stackoverflow.com/questions/6198104/reference-what-is-a-perfect-code-sample-using-the-mysql-extension) – Pekka Nov 27 '11 at 20:58
  • @Pekka: Do you see a `mysql_query` call? ;) – Ry- Nov 27 '11 at 20:59
  • 1
    @mini now that you say it! :) (Neil, the advice about error checking still holds true though) – Pekka Nov 27 '11 at 21:00
  • 1. You have errors in you SQL query. 2. You can't use `header()` then echo text (the browser will be expecting an jpeg image). – Nasreddine Nov 27 '11 at 21:00
  • 1
    possible duplicate of [Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result](http://stackoverflow.com/questions/795746/warning-mysql-fetch-array-supplied-argument-is-not-a-valid-mysql-result) – Kim Stebel Aug 09 '12 at 20:15

4 Answers4

5

You're not sending your query to the SQL database - it's just a string. You'll need to call mysql_query on it, like so:

$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");

As Pekka points out, though, you should be checking for errors as well.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • cheers for your help and i've now got it working. I stripped it to basics to get it working but have now put in error checks. But cheers for all your help. – Neil Grimmer Nov 29 '11 at 09:30
1

You are missing the mysql_query. The $result line should read the following:

$result = mysql_query($your_query_here);
Femaref
  • 60,705
  • 7
  • 138
  • 176
1

You are not querying your database …

$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'];
}
knittl
  • 246,190
  • 53
  • 318
  • 364
  • It's still a bad example because it doesn't check for errors. Just sayin' – Pekka Nov 27 '11 at 21:00
  • @Pekka: yeah, and it does not escape output (granted, it's a JPEG) and the query is still written in an awful way ;) – knittl Nov 27 '11 at 21:04
1

You forgot to use 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");