Jump to content





Recent Status Updates

  • Photo
      16 Apr
    Kadence

    If you're reading this, you're on my profile and I know you're on my profile because I'm probably viewing yours.

    Show comments (6)
  • Photo
      10 Apr
    Poe

    Finally (and hopefully) i'm getting a team together that knows a little of this and a little of that; and maybe all my open source projects that are half written can begin to be released. :)

View All Updates
- - - - -

Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

mysql

  • This topic is locked This topic is locked
213 replies to this topic

#1 Guest_Jaan_*

Guest_Jaan_*
  • Guest

Posted 29 April 2008 - 04:52 AM

Okay..I'm going to show y'all how to show your images.. those which are in your DB ;) It's very very simple.. so..

First of all we must connect to our database

<?php

$username = "";
$password = "";
$host = "localhost";
$database = "";
$username = ""; - It's your database's username
$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());

mysql_select_db($database) or die("Can not select the database: ".mysql_error());
mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error()); - This connects to your database

mysql_select_db($database) or die("Can not select the database: ".mysql_error());
- This will select your database

Now let's get our id

$id = $_GET['id'];
It will get a id from an URL. blabla.com
So.. your id will be 3 and it will show an image which id is 3

if(!isset($id) || empty($id) || !is_int($id)){
die("Please select your image!");
}else{
if(!isset($id) || empty($id) || !is_int($id)){ - If this ID in your url is empty like ?id= of if it's not even set
die("Please select your image!"); - lets display an error
}else{ - But if it is set let's continue with showing our image

Posted Image

$query = mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['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)
$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');
echo $content;
}
header('Content-type: image/jpg'); - This tells to the browser and to the server that this file will be a jpg file
echo $content; - This will display our blob..
} - Ends else

Okay.. now here's our full script.

<?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;
}

?>
It works perfectly :)
Posted Image

Enjoy :)
If you have questions then please feel free to ask :)

Attached Files


Edited by Alexander, 13 January 2011 - 02:58 AM.
Addressed SQL vulnerabilities

  • 0

#2 John

John

    CC Mentor

  • Moderator
  • 4,450 posts
  • Location:New York, NY

Posted 29 April 2008 - 02:39 PM

If `id` is always to be an integer, it would be wise to add:
if(!is_int($id) {
die("That image is not valid.");
}
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.
  • 2

#3 danchoivt

danchoivt

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 14 May 2008 - 01:28 AM

this is cool script, thanks mod too much
  • 0

#4 Guest_Jaan_*

Guest_Jaan_*
  • Guest

Posted 14 May 2008 - 01:32 AM

:D you're welcome
  • 0

#5 ClassicGlory

ClassicGlory

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 05 June 2008 - 02:11 PM

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
  • 0

#6 Guest_Jaan_*

Guest_Jaan_*
  • Guest

Posted 08 June 2008 - 01:59 AM

I believe.. then you should create a real images from those blobs.. use this tutorial to create those images:

http://forum.codecal...-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


unlink();


but..when you're creating a name for your file.. use

uniqid();


then when you have lot's of users it won't delete someone else image :D

i believe it works
  • 0

#7 ReekenX

ReekenX

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 123 posts

Posted 11 June 2008 - 11:15 PM

Well, great tutorial. +rep
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#8 Asinox

Asinox

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 26 June 2008 - 06:43 PM

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
  • 0

#9 coated_pill

coated_pill

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 10 July 2008 - 11:02 PM

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
  • 0

#10 coated_pill

coated_pill

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 10 July 2008 - 11:03 PM

if you need the script i would gladly show you .. i was wondering if i did the right thing on the MySQL dbase
  • 1

#11 Guest_Jaan_*

Guest_Jaan_*
  • Guest

Posted 11 July 2008 - 02:25 AM

yea you should show the script.. then we can solve the problem :)
  • 0

#12 coated_pill

coated_pill

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 11 July 2008 - 02:34 AM

i have solved the corrupt problem.. i just wanna ask if what will i place in the tags of files extension to uploads .zip or .rar in my webhost..

what will i replace with this tags
# What type of files should be allowed to be uploaded ?
$allow_type = "audio/mpeg";

Edited by coated_pill, 11 July 2008 - 02:46 AM.

  • 1