Jump to content

Picture Database


adi123

Recommended Posts

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
https://forums.phpfreaks.com/topic/210655-picture-database/#findComment-1099045
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
https://forums.phpfreaks.com/topic/210655-picture-database/#findComment-1099050
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
https://forums.phpfreaks.com/topic/210655-picture-database/#findComment-1099174
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
https://forums.phpfreaks.com/topic/210655-picture-database/#findComment-1099288
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.