Closed Thread
Page 1 of 22 12311 ... LastLast
Results 1 to 10 of 214

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

  1. #1
    Jaan Guest

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

    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
    Code:
    <?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

    Code:
    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

    Code:
    $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

    Code:
    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



    Code:
    $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

    Code:
    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.

    Code:
    <?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


    Enjoy
    If you have questions then please feel free to ask
    Attached Files Attached Files
    Last edited by Alexander; 01-13-2011 at 02:58 AM. Reason: Addressed SQL vulnerabilities

  2. CODECALL Circuit advertisement

     
  3. #2
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

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

    If `id` is always to be an integer, it would be wise to add:
    Code:
    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.

  4. #3
    danchoivt is offline Newbie
    Join Date
    May 2008
    Posts
    1
    Rep Power
    0

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

    this is cool script, thanks mod too much

  5. #4
    Jaan Guest

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

    you're welcome

  6. #5
    ClassicGlory is offline Newbie
    Join Date
    Jun 2008
    Posts
    1
    Rep Power
    0

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

    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

  7. #6
    Jaan Guest

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

    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

    Code:
    unlink(); 
    but..when you're creating a name for your file.. use

    Code:
    uniqid(); 
    then when you have lot's of users it won't delete someone else image

    i believe it works

  8. #7
    ReekenX's Avatar
    ReekenX is offline Programmer
    Join Date
    Jan 2007
    Location
    Lithuania
    Posts
    135
    Rep Power
    0

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

    Well, great tutorial. +rep

  9. #8
    Asinox is offline Newbie
    Join Date
    Jun 2008
    Posts
    1
    Rep Power
    0

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

    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

  10. #9
    coated_pill is offline Newbie
    Join Date
    Jul 2008
    Posts
    12
    Rep Power
    0

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

    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

  11. #10
    coated_pill is offline Newbie
    Join Date
    Jul 2008
    Posts
    12
    Rep Power
    0

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

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

Closed Thread
Page 1 of 22 12311 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 02-07-2011, 11:29 AM
  2. Tutorial: Storing Images in MySQL with PHP
    By Jordan in forum PHP Tutorials
    Replies: 47
    Last Post: 01-04-2011, 08:37 PM
  3. Beginner Storing images in XML file - Part II - Displaying image
    By Jaan in forum PHP Tutorials
    Replies: 6
    Last Post: 01-04-2011, 08:35 PM
  4. display images sequentially using PHP and MySQL
    By jhanjon in forum PHP Development
    Replies: 2
    Last Post: 10-08-2009, 12:11 PM
  5. Replies: 2
    Last Post: 07-16-2009, 12:48 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts