Retrieving image data from mysql using php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pravin3009
Forum Newbie
Posts: 3
Joined: Mon Aug 02, 2010 10:29 am

Retrieving image data from mysql using php

Post by pravin3009 »

I am trying to retrieve image (blob data ) from mysql, I able to do that successfully, the problem is it is not forming an image with the help of html image control. following the code that i am trying..

//following code from where I am retriving the image data is in getImage.php file -
<?php

mysql_connect("localhost:3308","user","password") or die("Unable to connect to SQL server");
@mysql_select_db("dbname") or die("Unable to select database");
$result=mysql_query("select * from tblName where rowid=1") or die("Can't perform Query");

$data=@MYSQL_RESULT($result,0,"content_image");

Header("Content-type: image/jpeg");
echo ("$data");

?>

and following is the code from the page I am calling this page.
<td>
<?php
echo "<img src=\"getImage.php\" height='200' width='400' />";
?>
</td>

this is first time I am programming php, and I am a two day old programmer, any help on this is highly appriciated. waiting for the responses.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Retrieving image data from mysql using php

Post by AbraCadaver »

Do you get any errors? If you view source in the browser, is there any image data there?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
pravin3009
Forum Newbie
Posts: 3
Joined: Mon Aug 02, 2010 10:29 am

Re: Retrieving image data from mysql using php

Post by pravin3009 »

no errors, this is what the view source show -
<td>
<!-- Image1 -->
<img src="getImage.php" height='200' width='400' />

</td >

But instead of doing
<?php
echo "<img src=\"getImage.php\" height='200' width='400' />";
?>
however, If i do
<?php
include "getImage.php"
?>
It shows the binary data into the <td> element. that makes me sure that the data is coming from the database. Also with the echo statement it show the image contol with a cross mark at top left corner, if I right click on the image control and say view properties, it shows me the url of "http://localhost/hello/getImage.php" and dimensions of the image as declared for the html img control.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Retrieving image data from mysql using php

Post by AbraCadaver »

Does the binary data contain any \ characters? If so, then the data was probably escaped before it was inserted (maybe magic_quotes_gpc), or it is being escaped automatically coming out (maybe magic_quotes_runtime). Try this:

Code: Select all

echo stripslashes($data); 
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
pravin3009
Forum Newbie
Posts: 3
Joined: Mon Aug 02, 2010 10:29 am

Re: Retrieving image data from mysql using php

Post by pravin3009 »

no help with that. I did not inserted images into the database by adding escape characters, what i did was, I used mysql command prompt to insert the values into the table and for inserting the image I used LoadImage or LoadFrom ( i dont remember exactly).

just for information in case, I get the following lines at the bottom of my html page -
No log handling enabled - turning on stderr logging Cannot find module (IP-MIB): At line 0 in (none) Cannot find module (IF-MIB): At line 0 in (none) Cannot find module (TCP-MIB): At line 0 in (none) Cannot find module (UDP-MIB): At line 0 in (none) Cannot find module (HOST-RESOURCES-MIB): At line 0 in (none) Cannot find module (SNMPv2-MIB): At line 0 in (none) Cannot find module (SNMPv2-SMI): At line 0 in (none) Cannot find module (NOTIFICATION-LOG-MIB): At line 0 in (none) Cannot find module (UCD-SNMP-MIB): At line 0 in (none) Cannot find module (UCD-DEMO-MIB): At line 0 in (none) Cannot find module (SNMP-TARGET-MIB): At line 0 in (none) Cannot find module (NET-SNMP-AGENT-MIB): At line 0 in (none) Cannot find module (DISMAN-EVENT-MIB): At line 0 in (none) Cannot find module (SNMP-VIEW-BASED-ACM-MIB): At line 0 in (none) Cannot find module (SNMP-COMMUNITY-MIB): At line 0 in (none) Cannot find module (UCD-DLMOD-MIB): At line 0 in (none) Cannot find module (SNMP-FRAMEWORK-MIB): At line 0 in (none) Cannot find module (SNMP-MPD-MIB): At line 0 in (none) Cannot find module (SNMP-USER-BASED-SM-MIB): At line 0 in (none) Cannot find module (SNMP-NOTIFICATION-MIB): At line 0 in (none) Cannot find module (SNMPv2-TM): At line 0 in (none)

is that causing the problem? I installed the php and mysql two days back, do I have to update the "ini" file for any settings?
Post Reply