Current location: Hot Scripts Forums » Programming Languages » PHP » php mysql help--cant retrieve an image from database


php mysql help--cant retrieve an image from database

Reply
  #1 (permalink)  
Old 10-18-10, 03:08 PM
salmander salmander is offline
New Member
 
Join Date: Oct 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy php mysql help--cant retrieve an image from database

hello there, Please help me this....
I am quite new to php....i am trying to develop this php page which uploads the image then it saves it in a mysql database. The data that is saved in database is saved as a mediumblob type but when I try to retrieve the image it doesnot shows an image instead a small box with a question mark in it.
this is my code:
index.php
PHP Code:

<html>
<head>
<title>Image Upload</title>
</head>

<body>

<form action="index.php" method="POST" enctype="multipart/form-data">
File: 
<input type="file" name="myfile">
<br>
<input type="submit" value="upload">
</form>

<?php
// conecting to database

mysql_connect("localhost","root","") or die("cant connect to server.");
mysql_select_db("databaseimage") or die("cant connect to database");



// file properties

/* here we are declaring a variable which uses a builtin function of $_FILES and the first part of it is the name of the image, which we have selected in input name ="image" section. and the second part is the temporary name and location where the file is being uploaded, usually its the temp folder of the server....you can check it by writing echo before the $file */

echo "name: ";
echo 
$name $_FILES['myfile']['name'];
echo 
"<BR>";
echo 
"type ";
echo 
$type $_FILES['myfile']['type'];
echo 
"<BR>";
echo 
"Size: ";
echo 
$size $_FILES['myfile']['size'];
echo 
"<BR>";
echo 
"temp: ";
echo 
$temp $_FILES['myfile']['tmp_name'];
echo 
"<BR>";
echo 
"error code: ";
echo 
$error $_FILES['myfile']['error'];
echo 
"<BR>";
echo 
"<BR>";
echo 
"file contents: "// this will be used to save in database as BLOB (Binary Large Object) data type.


echo $file_contents addslashes(file_get_contents($_FILES['myfile']['tmp_name']));

echo 
"<BR>";

// now inserting the data into database

if (!$insert mysql_query("INSERT INTO store VALUES ('','$name','$file_contents')"))
 echo 
"problem uploading file";
 
 else
 {

$last_id mysql_insert_id();
echo 
"Image uploaded.";
echo 
"<BR>";
echo 
"<img src=get.php?id=$last_id>";

 }

?>
</body>
</html>
-----get.php----

<?php
PHP Code:


// conecting to database

mysql_connect("localhost","root","") or die("cant connect to server.");
mysql_select_db("databaseimage") or die("cant connect to database");



$id addslashes($_REQUEST['id']);
$image mysql_query("SELECT * FROM store WHERE id = $id");
$image mysql_fetch_assoc($image);
$image $image['image'];


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


echo 
$image;

?> 

Last edited by job0107; 10-18-10 at 07:04 PM.
Reply With Quote
  #2 (permalink)  
Old 10-18-10, 07:02 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Your going about this the wrong way.
You don't want to save the image data in the database, just the file name of the image.
And save the image on the server in a file.
Use PHP's move_uploaded_file() command to save the file.
And then save the image's file name (and path) in the database.
Then you can display the image using an <img scr="path/imageFileName.jpg"> element.
__________________
Jerry Broughton
Reply With Quote
  #3 (permalink)  
Old 10-18-10, 07:06 PM
salmander salmander is offline
New Member
 
Join Date: Oct 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy

Quote:
Originally Posted by job0107 View Post
Your going about this the wrong way.
You don't want to save the image data in the database, just the file name of the image.
And save the image on the server in a file.
Use PHP's move_uploaded_file() command to save the file.
And then save the image's file name (and path) in the database.
Then you can display the image using an <img scr="path/imageFileName.jpg"> element.
ya i think, I have to take this approach because I am fed up with saving the data to the database...and then retrieving it...it is not working at all...i have followed the tutorials again and again...just to make sure that if i am not doing anything wrong... but the question is why it is not working??
Reply With Quote
Reply

Bookmarks

Tags
database, image, mysql, php


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with some php mysql TheTinkeringToad PHP 9 02-01-06 10:56 AM
Image to database with message and text vertigoflow PHP 3 01-20-06 12:46 PM
Simple PHP MYSQL database music_man PHP 3 05-02-04 02:54 AM
Generating PHP code for MySQL ptesone General Advertisements 1 02-03-04 08:26 AM
mySQL database - Image size reduction within DB trigger_my_passion PHP 0 01-29-04 04:24 PM


All times are GMT -5. The time now is 08:43 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.