0

Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

okay so here is my code.

<?php

include 'connect.php';


$id = addslashes($_REQUEST['id']);


$image = "SELECT * FROM images WHERE id=$id";
$result = mysql_query($image);
$imagearray = mysql_fetch_assoc($result);
$realimage = $imagearray['image'];

header("Content-type: image/jpeg");

echo $realimage;

?>

the page uses GET to get the id but then it gives me this error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/a6289454/public_html/get.php on line 9

Community
  • 1
  • 1
Matt Williams
  • 79
  • 1
  • 8

4 Answers4

3

var_dump(mysql_error()); will help you find the error message. But most likely thing is you need quotes:

$image = "SELECT * FROM `images` WHERE `id`='".$id."'";
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • I also suggest to use [`mysql_real_escape_string`](http://www.php.net/manual/en/function.mysql-real-escape-string.php) instead of `addslashes`. – Sony Santos Feb 12 '12 at 00:15
0

I think the problem might be in addslashes(), try mysql_real_escape_string() instead.

iDifferent
  • 2,190
  • 1
  • 14
  • 19
0

Why are you adding slashes to $_REQUEST['id']? It should be an integer

Try this code:

if(is_numeric($_REQUEST['id'])){

    $q = "SELECT * FROM images WHERE id = " . $id; // Don't put variables in strings!
    $rs= mysql_query($q) or die("There is an error in the query:<br>" . $q . "<br><br>The error is: ". mysql_error()); // Throw and error if there is one.
    $result = mysql_fetch_assoc($rs);

} else { die('Not a valid ID'); }

Also for good practice use require_once('connect.php'); rather than include 'connect.php';

Richard
  • 4,341
  • 5
  • 35
  • 55
0
 $image = "SELECT * FROM images WHERE id=$id";
 $result = mysql_query($image) or die(mysql_error());

Try that

Also for your $id use type casting it's a simple solution to a simple problem

 $id = (int) $_REQUEST['id'];
dead
  • 364
  • 2
  • 10