Jump to content

Picture Database


adi123

Recommended Posts

Can some please help.

 

I have a mysql database. In the table I have added a colum for pictures as VARCHAR and inserted the locations of the pictures. The pictures are in a seperate folder.

 

I am trying to display the images in a php file but can't see where I am going wrong.

Link to comment
Share on other sites

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 pics FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['image'];

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

}

 

 

 

Code may need to be rewritten

Link to comment
Share on other sites

There's a few things wrong with that script. First off, when posting code please use [php] or [code] tags; I've edited them in for you this time.

 

Setting $id and then checking if it's set right after is pointless, that will always be true. The advantage of isset is that you can check if a variable is set without it throwing an error message. If you want to directly output the image to the browser then you must output the contents of the image, not the path to the image. You code can be rewritten:

 

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());

if(!isset($_GET['id'] || empty($_GET['id']){
die("Please select your image!");
}else{

$query = mysql_query("SELECT pics FROM tbl_images WHERE id='".$_GET['id']."'");
$row = mysql_fetch_array($query);
readfile($row['image']);
header('Content-type: image/jpg');
}

 

I would also suggest that your code more fault safe and secure by 1. escaping the input and 2. Making sure that a row was found before you attempt to use one.

Link to comment
Share on other sites

If anyone please knows how to write a tutorial or script, that would help a lot.

 

All i need to do is connect to the mysql database and take the location of the images from the database. The image files are stored in a folder called pics.

I am trying to display the images in a web page.

 

Can anyone please provide help on how to do this from start to finish.

 

Thanks.

Link to comment
Share on other sites

Did you even try what I posted? It should work as long as the path is correct...

 

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());

if(!isset($_GET['id'] || empty($_GET['id']){
die("Please select your image!");
}else{

$query = mysql_query("SELECT pics FROM tbl_images WHERE id='".$_GET['id']."'");
$row = mysql_fetch_array($query);
readfile("pics/" . $row['image']);
header('Content-type: image/jpeg');
}

 

If you're uploading images with formats other than jpeg you'll need to adjust the header accordingly.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.