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">
</form>
this is my insert.php that processes the image after its submitted
<?php
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);
fclose($fp);
$query = "INSERT INTO tbl_images ";
$query .= "(image) VALUES ('$data')";
$results = mysql_query($query, $link);
print "Thank you, your file has been uploaded.";
}else{
print "No image selected/uploaded";
}
mysql_close($link);
?>
and heres the show.php that displays the image using the id
<?php
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!");
}else{
$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
13
Other forums
Request.UrlReferrer
I need to make it so that a page can only be loaded when a button is clicked on a specific page. I d
Undefined Index Notice In Internet Explorer Only
I am getting the following notice:
QuoteNotice: Undefined index: jrox in /home/ycsn/public_h
login box shows up again after logging in 2nd login works
Hello,
I am having trouble on my site Lockerz.rack111.com where when you login the login box
Retreiving objects from Sessions
Hi all,
I am getting really frustrated with storing and retreiving objects from a session. Bu
Sales orders ans reservations not taken into account on MPS / MRP
Hello,
I am implementing the MPS / MRP and I have the following problem:
I ha
Variables and Include
Code: [Select]<?php
$header = $_COOKIE['mss']['header'];
$body = $_COOKIE['mss']['b
Error in query: Resource id #4??
hey guys, having a minor (i think) problem here that i havent been able to figure out. long story sh
Image DPI
Hi.
I allow users to upload images, the user then is able to adjust several settings for the
Getting the full city list from maxminds geoip database
I am suprised I can't find any references to this.
I have maxminds geoip lite installed and i
need help in php variable
i have a php variable on one page
Code: [Select]$lastId = mysql_insert_id($db);
echo $lastId;<