0

I followed a tutorial to display image from the database, this is what the table looks like in my database. What am I doing wrong?

Display
+-------+------------+----------+
| Index | Display_ID | Picture  |
+-------+------------+----------+
|     1 |         12 | longblob |
+-------+------------+----------+

 

<?php
if (!function_exists("GetSQLValueString"))
 {
function GetSQLValueString($theValue, $theType, 
$theDefinedValue = "", $theNotDefinedValue = "")
{
 // function definition omitted
}
}

$colname_getImage = "-1";
if (isset($_GET['image_id'])) 
{
  $colname_getImage = $_GET['image_id'];
}
$db = mysql_connect("localhost", "root");
mysql_select_db("draftdb",$db);
$query_getImage = sprintf("SELECT mimetype, PICTURE FROM display
WHERE DISPLAY_ID = %s", GetSQLValueString($colname_getImage, "int"));
$getImage = mysql_query($query_getImage, $db) or
die(mysql_error());
$row_getImage = mysql_fetch_assoc($getImage);
$totalRows_getImage = mysql_num_rows($getImage);
mysql_free_result($getImage);

header('Content-type: image/jpeg ' . $row_getImage['mimetype']);
echo $row_getImage['image'];

?>

> <img src="show_image.php?image_id=12 <?php echo
> $row_getdetails['image_id']; ?>" alt="Image from DB" />

ERROR: > You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2

Peon
  • 7,902
  • 7
  • 59
  • 100
Undermine2k
  • 1,481
  • 4
  • 28
  • 52
  • echo $query_getImage before executing it. Rule number 1, 2 and 3 when debugging dynamically created queries: echo them and execute them directly against your DB, and find what you do wrong. – Erwin Moller Jul 27 '12 at 07:19
  • I'm sorry I'm rather new to PHP where do I execute this line, can you explain it in more detail? – Undermine2k Jul 27 '12 at 07:24
  • Welcome to Stack Overflow! Please, don't use `mysql_*` functions for new code. They are no longer maintained and the community has begun the [deprecation process](http://goo.gl/KJveJ). See the [**red box**](http://goo.gl/GPmFd)? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide, [this article](http://goo.gl/3gqF9) will help to choose. If you care to learn, [here is good PDO tutorial](http://goo.gl/vFWnC). – Madara's Ghost Jul 27 '12 at 07:25
  • @Undermine2K Just before the line that starts with: $getImage = mysql_query($query_getImage, $db) or die(mysql_error()); Simply echo $query_getImage; exit; Also: make sure you follow the advice above by Truth. mysql_* functions should be avoided. – Erwin Moller Jul 27 '12 at 07:29
  • Oh wow thanks Truth for letting me know before I coded an entire project with mysql_* . I'll read up on the new options thanks!. – Undermine2k Jul 27 '12 at 07:31
  • 1
    Why do you have two almoust identical questions up? http://stackoverflow.com/questions/11683989/displaying-image-from-mysql-with-php – Peon Jul 27 '12 at 08:38
  • @DainisAbols, thanks - I'll report this one as the dup, since the newer one is a mysqli rewrite. – halfer Jul 27 '12 at 09:02

1 Answers1

0

Well, as far as I see, there is no field mimetype in your table. That might cause the error + if your database has field Picture, don't ask for a field named PICTURE.

Peon
  • 7,902
  • 7
  • 59
  • 100