Okay..I'm going to show y'all how to show your images.. those which are in your DBIt's very very simple.. so..
First of all we must connect to our database
$username = ""; - It's your database's usernameCode:<?php
$username = "";
$password = "";
$host = "localhost";
$database = "";
$password = ""; - It's your database's password
$host = "localhost"; - It's your database's host, usually it's localhost but it can be something else also
$database = ""; - It's your database
Now let's connect to the database
mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error()); - This connects to your databaseCode: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());
mysql_select_db($database) or die("Can not select the database: ".mysql_error()); - This will select your database
Now let's get our id
It will get a id from an URL. blabla.comCode:$id = $_GET['id'];
So.. your id will be 3 and it will show an image which id is 3
if(!isset($id) || empty($id) || !is_int($id)){ - If this ID in your url is empty like ?id= of if it's not even setCode:if(!isset($id) || empty($id) || !is_int($id)){
die("Please select your image!");
}else{
die("Please select your image!"); - lets display an error
}else{ - But if it is set let's continue with showing our image
$query = mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'"); - Now let's select the blob from the table where id is $id (for example: 3)Code:$query = mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['image'];
$row = mysql_fetch_array($query); - Let's gather our info about image which id is $id into one variable
$content = $row['image']; - Get's the blob from our table
Now let's display our image
header('Content-type: image/jpg'); - This tells to the browser and to the server that this file will be a jpg fileCode:header('Content-type: image/jpg');
echo $content;
}
echo $content; - This will display our blob..
} - Ends else
Okay.. now here's our full script.
It works perfectlyCode:<?php
$username = "";
$password = "";
$host = "localhost";
$database = "";
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) || !is_int($id)){
die("Please select your image!");
}else{
$query = mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['image'];
header('Content-type: image/jpg');
echo $content;
}
?>
Enjoy
If you have questions then please feel free to ask![]()
Last edited by Alexander; 01-13-2011 at 02:58 AM. Reason: Addressed SQL vulnerabilities
If `id` is always to be an integer, it would be wise to add:That way your code is not nearly as vulnerable to SQL injections. I would also add mysql_real_escape_string($id) inside the query too - but not absolutely necessary. Other than that, nice tutorial.Code:if(!is_int($id) {
die("That image is not valid.");
}
this is cool script, thanks mod too much
you're welcome
Jaan,
I loved your easy to follow instructions but I want to do something slightly different. I have a MySQL db and want to display a Blob image from it into a table populated by PHP.
I have a MySQL query which selects all records from the database and then displays the text content from each record on a row in the table.
I want one of the table cells in each row to display the blob image.
How do I do this?
Regards
CG
I believe.. then you should create a real images from those blobs.. use this tutorial to create those images:
http://forum.codecall.net/php-tutori...files-php.html
there.. this blob will be this $content.. so replace it..
and then.. after your script ends.. just delete those images.. then just use
but..when you're creating a name for your file.. useCode:unlink();
then when you have lot's of users it won't delete someone else imageCode:uniqid();
i believe it works
Well, great tutorial. +rep
Hi everybody, sorry with my english... this is my first post here....
i hav a question
¿how ill show all images from my DDBB where i hav user ID?
this tutorial show just 1 images ....but if i hav a lot of picture and i need to show all of the USER ID x?
im trying to do a while but i cant do this work
Some Help?
Thanks
i have a script to show thumbnails in table but the image become corrupted in the net.. how would i solve this problem?? please help me thank you
if you need the script i would gladly show you .. i was wondering if i did the right thing on the MySQL dbase
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks