Display image in MYSQL

acellec

New Member
Messages
29
Reaction score
0
Points
0
hello i added a field in my table for images with field type long blob...how can i display it in my php code.. ihave this in my code:

Row 6 is the field for images please help....

i have added this code to display but it doenst show any image ..thanks
echo "<td align='right'><img src=\"$Row[6]\"alt=\"\"/></td>";

PHP:
<?

$TableName = "automobileparts";
$SQLstring = "SELECT * FROM automobileparts";

require_once("dbconnect.php");



        
echo "<table width='100%' border='1'>";
$Row = $QueryResult->fetch_row();
do {
             echo "<tr><td>{$Row[0]}</td>";
        
        echo "<td align='right'>{$Row[1]}</td>";
        echo "<td align='right'>{$Row[2]}</td>";
        echo "<td align='right'>{$Row[3]}</td>";
        echo "<td align='right'>{$Row[4]}</td>";
        echo "<td align='right'>{$Row[5]}</td>"; 
        echo "<td align='right'><img src=\"$Row[6]\"alt=\"\"/></td>"; 
        echo "<td align='right'><a href=\"Mail2.php?id={$Row[0]}\">Order</a></td>";   
                       
                   echo "</tr>";
        $Row = $QueryResult->fetch_row();
} while ($Row);
echo "</table>";



$DBConnect->close();


?>
 
Last edited:

garrettroyce

Community Support
Community Support
Messages
5,611
Reaction score
249
Points
63
You have to use the header() function to set the content-type as image/[your image type]

Since you want to use the image in a page that has more than just an image, you have to actually use two different scripts. Then, you will access your images like this:

<img src="get_image.php?image=$image_number" />

$image_number should be a unique id for your image. If your table isn't set up this way, add a row, integer type, unsigned, auto increment, primary key.

Then, getting the image is as easy as this: http://bytes.com/groups/php/1655-php-mysql-image-blobs#post4918
 

acellec

New Member
Messages
29
Reaction score
0
Points
0
You have to use the header() function to set the content-type as image/[your image type]

Since you want to use the image in a page that has more than just an image, you have to actually use two different scripts. Then, you will access your images like this:

<img src="get_image.php?image=$image_number" />

$image_number should be a unique id for your image. If your table isn't set up this way, add a row, integer type, unsigned, auto increment, primary key.

Then, getting the image is as easy as this: http://bytes.com/groups/php/1655-php-mysql-image-blobs#post4918

it doest display can you check my code

PHP:
<?

$TableName = "automobileparts";
$SQLstring = "SELECT * FROM automobileparts";

require_once("dbconnect.php");



header ("Content-type: image/jpeg");        
echo "<table width='100%' border='1'>";
$Row = $QueryResult->fetch_row();
do {
                 
        echo "<tr><td>{$Row[0]}</td>";
        echo "<td align='right'>{$Row[1]}</td>";
        echo "<td align='right'>{$Row[2]}</td>";
        echo "<td align='right'>{$Row[3]}</td>";
        echo "<td align='right'>{$Row[4]}</td>";
        echo "<td align='right'>{$Row[5]}</td>";
        echo "<td align='right'><img src="get_image.php?image=$Row[1]" /></td>";
        echo "<td align='right'></td>"; 
        echo "<td align='right'><a href=\"Mail2.php?id={$Row[0]}\">Order</a></td>";   
                       
                   echo "</tr>";
        $Row = $QueryResult->fetch_row();
} while ($Row);
echo "</table>";



$DBConnect->close();


?>
 

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
"header ("Content-type: image/jpeg");" should not be in the script printing the html, it should be in the script printing the image. Setting the header as an image ensures that the image data printed in get_image.php displays as an image rather than a bunch of incomprehensible characters.
 
Last edited:

acellec

New Member
Messages
29
Reaction score
0
Points
0
"header ("Content-type: image/jpeg");" should not be in the script printing the html, it should be in the script printing the image. Setting the header as an image ensures that the image data printed in get_image.php displays as an image rather than a bunch of incomprehensible characters.


is it like this then ? still doesnt display..can you show me please

PHP:
echo "<table width='100%' border='1'>";
$Row = $QueryResult->fetch_row();
do {
                 
        echo "<tr><td>{$Row[0]}</td>";
        echo "<td align='right'>{$Row[1]}</td>";
        echo "<td align='right'>{$Row[2]}</td>";
        echo "<td align='right'>{$Row[3]}</td>";
        echo "<td align='right'>{$Row[4]}</td>";
        echo "<td align='right'>{$Row[5]}</td>";
        
        header("Content-type: image/jpeg");
        print $Row[6];
        echo "<td align='right'></td>"; 
        echo "<td align='right'><a href=\"Mail2.php?id={$Row[0]}\">Order</a></td>";   
                       
                   echo "</tr>";
        $Row = $QueryResult->fetch_row();
} while ($Row);
echo "</table>";
 
Last edited:

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
no, what you wrote will not work, since you a outputing both an image and some html. What Twikie meant was that the header('content-type: image/jpeg') should be in the get_image.php file. Also, instead of doing a do...while loop, you should do a while loop. Way better, if, for example, there are no results. And it will save you a couple lines. Oh and you should seriously consider using names instead of numerical indices. It's way clearer.
PHP:
<?php
//display car info.php

$TableName = "automobileparts";
$SQLstring = "SELECT * FROM automobileparts";

require_once("dbconnect.php");

     
echo "<table width='100%' border='1'>";
while ($Row= $QueryResult->fetch_row()) {
                 
        echo "<tr><td>{$Row[0]}</td>";
        echo "<td align='right'>{$Row[1]}</td>";
        echo "<td align='right'>{$Row[2]}</td>";
        echo "<td align='right'>{$Row[3]}</td>";
        echo "<td align='right'>{$Row[4]}</td>";
        echo "<td align='right'>{$Row[5]}</td>";
        echo "<td align='right'><img src=\"get_image.php?image={$Row[1]}\" /></td>";
        echo "<td align='right'></td>"; 
        echo "<td align='right'><a href=\"Mail2.php?id={$Row[0]}\">Order</a></td>";             
        echo "</tr>";
}

echo "</table>";

$DBConnect->close();
?>
Next, in the get_image.php file:
PHP:
<?php
//get_image.php

// you need to sanitize it yourself
$image_id = $_GET['image'];

$TableName = "automobileparts";
$SQLstring = "SELECT * FROM automobileparts WHERE id = $image_id LIMIT 1";

require_once("dbconnect.php");

header('Content-Type: image/jpeg');
$Row= $QueryResult->fetch_row()
echo $Row[6];
$DBConnect->close();
?>
 

acellec

New Member
Messages
29
Reaction score
0
Points
0
no, what you wrote will not work, since you a outputing both an image and some html. What Twikie meant was that the header('content-type: image/jpeg') should be in the get_image.php file. Also, instead of doing a do...while loop, you should do a while loop. Way better, if, for example, there are no results. And it will save you a couple lines. Oh and you should seriously consider using names instead of numerical indices. It's way clearer.
PHP:
<?php
//display car info.php

$TableName = "automobileparts";
$SQLstring = "SELECT * FROM automobileparts";

require_once("dbconnect.php");

     
echo "<table width='100%' border='1'>";
while ($Row= $QueryResult->fetch_row()) {
                 
        echo "<tr><td>{$Row[0]}</td>";
        echo "<td align='right'>{$Row[1]}</td>";
        echo "<td align='right'>{$Row[2]}</td>";
        echo "<td align='right'>{$Row[3]}</td>";
        echo "<td align='right'>{$Row[4]}</td>";
        echo "<td align='right'>{$Row[5]}</td>";
        echo "<td align='right'><img src=\"get_image.php?image={$Row[1]}\" /></td>";
        echo "<td align='right'></td>"; 
        echo "<td align='right'><a href=\"Mail2.php?id={$Row[0]}\">Order</a></td>";             
        echo "</tr>";
}

echo "</table>";

$DBConnect->close();
?>
Next, in the get_image.php file:
PHP:
<?php
//get_image.php

// you need to sanitize it yourself
$image_id = $_GET['image'];

$TableName = "automobileparts";
$SQLstring = "SELECT * FROM automobileparts WHERE id = $image_id LIMIT 1";

require_once("dbconnect.php");

header('Content-Type: image/jpeg');
$Row= $QueryResult->fetch_row()
echo $Row[6];
$DBConnect->close();
?>


from the car info.php i just change this part to {$Row[0]}...since it's the part number id for the pictures

PHP:
echo "<td align='right'><img src=\"get_image.php?image={$Row[1]}\" /></td>";

and from the get_image.php

i change this part id to PartNo.

PHP:
$SQLstring = "SELECT * FROM automobileparts WHERE id = $image_id LIMIT 1";

i dont have errors but the picture still doesnt display..can you help me?
Edit:
myabe something wrong when i add a field image in mysql? what do you think?
 
Last edited:

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
What displays when you follow the direct link to the php image file?
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
what kind of characters. There are loads of characters. Copy them here or give us some link that we can chew on...
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Even better, post a URL so the forum character encoding won't munge the data and we can examine the HTTP headers. Also, read a tutorial on storing and serving images from MySQL to get a better understanding of what's going on.

Did you follow xav0989's comment and sanitize $_GET['image'] with (e.g.) filter_var or mysql_escape_string? You don't want to suffer the consequences if you don't.
 
Last edited:

acellec

New Member
Messages
29
Reaction score
0
Points
0
Even better, post a URL so the forum character encoding won't munge the data and we can examine the HTTP headers. Also, read a tutorial on storing and serving images from MySQL to get a better understanding of what's going on.

Did you follow xav0989's comment and sanitize $_GET['image'] with (e.g.) filter_var or mysql_escape_string? You don't want to suffer the consequences if you don't.

can you show me how to sanitize?..

here is my link..

http://acemysql.freetzi.com/IndexProject.php
 
Last edited:

acellec

New Member
Messages
29
Reaction score
0
Points
0
attached is my sql fields and how i uploaded my images.. help me please
 

Attachments

  • mysql dbase.JPG
    mysql dbase.JPG
    31.2 KB · Views: 38
  • mysql dbase2.JPG
    mysql dbase2.JPG
    32 KB · Views: 35

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
can you show me how to sanitize?..
There are plenty of forum posts, tutorials and examples in the PHP documentation that cover sanitization. Go over those and update your code. Alternatively, use prepared statements (with e.g. mysqli::prepare). If you're having problems, read "How To Ask Questions The Smart Way" and follow its guidelines to start a new thread.

You should also read over some articles on SQL Injection (2 3) so you understand why you're sanitizing input.

IndexProject.php is still embedding binary image data in the HTML output. Dumping image data into the HTML output of a script will always fail. HTML is a text only format. Apply xav0989's fix of printing an <img> element whose src attribute points to a page that prints the image. I was thus expecting you to post the URL of the image printing page, something like:
The above gives a parse error:
Parse error: syntax error, unexpected T_ECHO in /home/vhosts/acemysql.freetzi.com/get_image.php on line 14
It looks like xav0989's get_image.php had a missing semicolon on the "fetch_row()" line. It should read:
PHP:
$Row= $QueryResult->fetch_row();
 
Last edited:

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
It looks like xav0989's get_image.php had a missing semicolon on the "fetch_row()" line. It should read:
PHP:
$Row= $QueryResult->fetch_row();
Oops, my bad... should have read over my code... :biggrin:
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
At this point, whenever I think "This is a short script, I'll just post it," I force myself to test it. Invariably there's a typo.
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
I should start doing it, as well.

Oh BTW, mission, can I quote your signature? I find it very to the point nowadays...
 
Last edited:

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
Now it justs echo's the file name... Could you post the content of the get_image.php script here again?

I made a few changes to my script:
PHP:
<?php
//get_image.php

// you need to sanitize it yourself
$image_id = $_GET['image'];

$TableName = "automobileparts";
$SQLstring = "SELECT * FROM automobileparts WHERE PartNo = $image_id LIMIT 1";

require_once("dbconnect.php");

header('Content-Type: image/jpeg');
$Row = $QueryResult->fetch_row();
echo $Row[5];
$DBConnect->close();
?>
If you can change the fetch_row() function to use names instead of numbers, it would be great.
 
Last edited:

acellec

New Member
Messages
29
Reaction score
0
Points
0
Now it justs echo's the file name... Could you post the content of the get_image.php script here again?

I made a few changes to my script:
PHP:
<?php
//get_image.php

// you need to sanitize it yourself
$image_id = $_GET['image'];

$TableName = "automobileparts";
$SQLstring = "SELECT * FROM automobileparts WHERE PartNo = $image_id LIMIT 1";

require_once("dbconnect.php");

header('Content-Type: image/jpeg');
$Row = $QueryResult->fetch_row();
echo $Row[5];
$DBConnect->close();
?>
If you can change the fetch_row() function to use names instead of numbers, it would be great.

here is the code:

PHP:
<?php
//get_image.php

// you need to sanitize it yourself
$image_id = $_GET['image'];

$TableName = "automobileparts";
$SQLstring = "SELECT * FROM automobileparts WHERE PartNo = '$image_id'";

require_once("dbconnect.php");

header('Content-Type: image/jpeg');
$Row= $QueryResult->fetch_row();
echo $Row[6]; 
//$Row[6] is $Row['bmkimage'] is the image itself

$DBConnect->close();
?>
 
Top