retrieving images from mysql database using php

Posted on 16th Feb 2014 07:03 pm by admin

So I've been trying to figure out how to store images in a mysql database, and as far as i can tell the images are stored but getting them out seems to be the problem.

when i try to go to the page on my webhost it says that

"Can not select the database: Access denied for user 'testimg'@'localhost' to database 'testImages'"

and when i goto show.php?id=1 on my local mamp install it gives me all kinds of weird symbols

ÿÀ�™Ì�ÿÄ�È�������������������������� �!1AQaq"2‘¡±BR#Ábr‚’Ñ¢Â3CS$á²Òsƒ“%ðñc£Ã4â³T56Dt”E&Ód¤´ÄUu•7���!1AQaqð‘¡"2±ÁÑáñBRÂbr⢉#3ÿÚ� ��?�Ü/é -è

i've checked everything in the code a million times and searched google and this forum for anything that can help me but i haven't been able to find something that has helped me understand what exactly is going on and why

heres the upload form

<form enctype="multipart/form-data" action="insert.php" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="1500000" type="hidden">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">

this is my insert.php that processes the image after its submitted


include './database.php';

$link = mysql_connect($host, $username, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());

mysql_select_db ($database);

if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {

$tmpName = $_FILES['image']['tmp_name'];

$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));

$data = addslashes($data);

$query = "INSERT INTO tbl_images ";
$query .= "(image) VALUES ('$data')";
$results = mysql_query($query, $link);

print "Thank you, your file has been uploaded.";

print "No image selected/uploaded";


and heres the show.php that displays the image using the id


include './database.php';

@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

@mysql_select_db($database) or die("Can not select the database: ".mysql_error());

$id = $_GET['id'];

if(!isset($id) || empty($id)){
die("Please select your image!");

$query = mysql_query("SELECT image FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_assoc($query);
$content = $row['image'];

header("Content-type: image/jpg");
echo $content;



No comments posted yet

Your Answer:

Login to answer
244 Like 13 Dislike
Previous forums Next forums
Other forums

noob cURL help
I have a pretty basic form that I need to cURL post to a file in my includes folder (includes/login.

This is driving me nuts!
This insert query looks to be alright, however I get this error:

QuoteYou have an error in yo

writing my own sobel filter convolution - something is wrong
I am trying to keep it very simple, I cant see anything wrong with my logic, could anybody help poin

Using Curl_multi for processing multiple URLs

I am at a loss as to how to implement this. I would like to be able to automatically assi

path to include folder
Hi everyone,

When I'm testing locally, I have to add the name of the site folder as part of t

Working with popups and such in a class
I've recently started building my applications completely enclosed in classes. I like the fact that

Apart from cron
I need to run a php file every one hour. Is there any other solution apart from cron job?

Simple PHP/Java Help..
I'm trying to build a little program to do with calculating postage. Unfortunately i haven't

[function.file]: failed to open stream: No such file or directory
Okay I wrote this little script to upload a csv file that is located on my webhosts server to a mysq

I have a variable in my PHP script like


Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS

Play Free Quiz and Win Cash