+ Reply to Thread
Page 1 of 16
1 2 3 11 ... LastLast
Results 1 to 10 of 156

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

  1. #1
    Moderator Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan's Avatar
    Join Date
    Dec 2006
    Location
    Estonia
    Age
    18
    Posts
    2,048
    Blog Entries
    9

    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)){
    die(
    "Please select your image!");
    }else{ 
    if(!isset($id) || empty($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)){
    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
    Last edited by Jordan; 05-07-2008 at 12:02 PM.
    Trill Hosting - Cheap Web Hosting, Register Cheap Domains, Cheap Blog Hosting
    www.trillhosting.com | support@trillhosting.com
    Hosting Plans | Write To Us | Support | Client Area | About Us
    CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | Freelance

  2. #2
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,883
    Blog Entries
    25

    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.

  3. #3
    Newbie danchoivt is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    1

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

    this is cool script, thanks mod too much

  4. #4
    Moderator Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan's Avatar
    Join Date
    Dec 2006
    Location
    Estonia
    Age
    18
    Posts
    2,048
    Blog Entries
    9

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

    you're welcome
    Trill Hosting - Cheap Web Hosting, Register Cheap Domains, Cheap Blog Hosting
    www.trillhosting.com | support@trillhosting.com
    Hosting Plans | Write To Us | Support | Client Area | About Us
    CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | Freelance

  5. #5
    Newbie ClassicGlory is an unknown quantity at this point
    Join Date
    Jun 2008
    Posts
    1

    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

  6. #6
    Moderator Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan is a splendid one to behold Jaan's Avatar
    Join Date
    Dec 2006
    Location
    Estonia
    Age
    18
    Posts
    2,048
    Blog Entries
    9

    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:

    Create files with php

    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
    Trill Hosting - Cheap Web Hosting, Register Cheap Domains, Cheap Blog Hosting
    www.trillhosting.com | support@trillhosting.com
    Hosting Plans | Write To Us | Support | Client Area | About Us
    CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | Freelance

  7. #7
    Programmer ReekenX is an unknown quantity at this point ReekenX's Avatar
    Join Date
    Jan 2007
    Posts
    133

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

    Well, great tutorial. +rep

  8. #8
    Newbie Asinox is an unknown quantity at this point
    Join Date
    Jun 2008
    Posts
    1

    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

  9. #9
    Newbie coated_pill is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    12

    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

  10. #10
    Newbie coated_pill is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    12

    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

+ Reply to Thread
Page 1 of 16
1 2 3 11 ... LastLast

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Tutorial: Storing Images in MySQL with PHP
    By Jordan in forum PHP Tutorials
    Replies: 43
    Last Post: 01-16-2010, 10:35 AM
  2. Tutorial: PHP to MySQL
    By Jordan in forum PHP Tutorials
    Replies: 20
    Last Post: 08-28-2009, 04:22 AM

Posting Permissions

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