By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,905 Members | 1,585 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,905 IT Pros & Developers. It's quick & easy.

displaying images.

P: 53
i used a code from a website that allows you to display images. however everything works fine from storing the image to the database but it does not display the image.

the following code is the one i have used.

Storing the images:

[PHP]<HTML>
<HEAD><TITLE>Store binary data into SQL Database</TITLE></HEAD>
<BODY>

<?php

if (isset($_REQUEST['submit'])) {

mysql_connect("localhost","root","");
mysql_select_db("binary_data");

$data = addslashes(fread(fopen($_FILES['form_data']['tmp_name'], "r"), $_FILES['form_data']['size']));

$result= mysql_query("INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ".
"VALUES ('".$form_description."','".$data."','".$_FILES['form_data']['name']."','".$_FILES['form_data']['size']."','".$_FILES['form_data']['type']."')");

$id= mysql_insert_id();
print "<p>This file has the following Database ID: <b>$id</b>";

mysql_close();

} else {
?>
<form method="post" action="store.php" enctype="multipart/form-data">
File Description:<br>
<input type="text" name="form_description" size="40">
<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000000">
<br>File to upload/store in database:<br>
<input type="file" name="form_data">
<p><input type="submit" name="submit" value="submit">
</form>

<?php

}

?>[/PHP]

Getting the image from database :

[PHP]<?php

if($id) {

mysql_connect("localhost","root","");

mysql_select_db("binary_data");

$query = "select bin_data,filetype from table where id='$id'";
$result = mysql_query($query);

$data = mysql_result($result,0,"bin_data");
$type = mysql_result($result,0,"filetype");

header( "Content-type: $type");
header( "Content-type: image/pjpeg");
echo $data;


};
?>[/PHP]

displaying the image:

[PHP]<html>
<head>
<title>images retrieved from database</title>
</head>

<body>

<img src="getdata.php?id=3">

</body>
</html>[/PHP]

if someone can help i would greatly appreciate it.

thanks

ashraf
Mar 21 '08 #1
Share this Question
Share on Google+
14 Replies


hsriat
Expert 100+
P: 1,654
Open getdata.php?id=3 in your browser.
See what error it gives.

It should be $_GET['id'] instead of $id
Mar 22 '08 #2

Markus
Expert 5K+
P: 6,050
Open getdata.php?id=3 in your browser.
See what error it gives.

It should be $_GET['id'] instead of $id
More over, should be:
[php]
$id = $_GET['id']; # do some cleaning of this.
[/php]
You should, also, sanitize that input; it could be malicious.

Regards.
Mar 22 '08 #3

P: 53
thanks for ur reply guys

but still no luck i'v tried both ways. i'm new to php and i have an assignment to hand in soon. anymore suggestion.

do u think storing the url to the images would be easier?

thanks in advance.

ashraf
Mar 22 '08 #4

hsriat
Expert 100+
P: 1,654
thanks for ur reply guys

but still no luck i'v tried both ways. i'm new to php and i have an assignment to hand in soon. anymore suggestion.

do u think storing the url to the images would be easier?

thanks in advance.

ashraf
What error do you get when you open getdata.php?id=3 in your browser?
Mar 22 '08 #5

P: 53
i get no error just a blank screen
Mar 22 '08 #6

P: 53
What error do you get when you open getdata.php?id=3 in your browser?
sorry i only put in getdata.php not getdata.php?id=3

this is the following erros i get on the page.


Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\wamp\www\getdata.php on line 13

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\wamp\www\getdata.php on line 14

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\getdata.php:13) in C:\wamp\www\getdata.php on line 16

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\getdata.php:13) in C:\wamp\www\getdata.php on line 17
Mar 22 '08 #7

P: 53
i've sorted the problem out. i looked at the errors and figured out wat was wrong in mysql_query it said select from table instead of the actual table name.

thanks anyway i wouldn't have found that out if u didn't tell me to look at the errors on that particular script. nice one "hsriat" and "markusn00b"
Mar 22 '08 #8

Markus
Expert 5K+
P: 6,050
Try using a while loop to assign the data:
[php]
$_res = mysql_query("SELECT .... ");
while($_rows = mysql_fetch_array($_res))
{
$_data = $_rows['bin_data'];
$_file = $_rows['filetype'];
}
[/PHP]

Regards.

EDIT: Neglect this then.

Glad you've sorted it!
Mar 22 '08 #9

ronverdonk
Expert 2.5K+
P: 4,258
Do not post duplicate threads in this forum! I does not help you in any way. It just annoys people.

The duplicate thread has been removed.

moderator
Mar 22 '08 #10

P: 53
i didn't duplicate the thread. next time b4 u delete a post read the code and maybe compare the two codes and check. this code was for BLOB files and the other was for linking url files.
Mar 22 '08 #11

hsriat
Expert 100+
P: 1,654
i didn't duplicate the thread. next time b4 u delete a post read the code and maybe compare the two codes and check. this code was for BLOB files and the other was for linking url files.
In the code you provided in the other thread (removed one), view source of your page in a browser (recommended FF), and see what is the problem with your img tag's src attribute.
Check if the referred source actually exists.
Mar 22 '08 #12

P: 53
thanx "HSRIAT" but i have sorted the problem out i did wat u said and it seemed that i forgot to put a forward slash in the code thanks any way much appreciated.
Mar 22 '08 #13

Markus
Expert 5K+
P: 6,050
i didn't duplicate the thread. next time b4 u delete a post read the code and maybe compare the two codes and check. this code was for BLOB files and the other was for linking url files.
You DID duplicate the thread.

I read the new thread, fully, and it was in no-way different from this thread.

Even it a question was partly linked to this thread, you should keep it on the same thread.

Regards,
Mar 23 '08 #14

hsriat
Expert 100+
P: 1,654
You DID duplicate the thread.

I read the new thread, fully, and it was in no-way different from this thread.

Even it a question was partly linked to this thread, you should keep it on the same thread.

Regards,
It was a bit different. In that one, he was saving the name of the image in the db, and in this one, image is saved in db.

Though I'm sorry for argument.

:)
Harpreet
Mar 23 '08 #15

Post your reply

Sign in to post your reply or Sign up for a free account.