Display image in MYSQL

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by acellec, Jun 17, 2009.

  1. acellec

    acellec New Member

    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    hello i added a field in my table for images with field type long blob...how can i display it in my php code.. ihave this in my code:

    Row 6 is the field for images please help....

    i have added this code to display but it doenst show any image ..thanks
    echo "<td align='right'><img src=\"$Row[6]\"alt=\"\"/></td>";

    PHP:
    <?

    $TableName "automobileparts";
    $SQLstring "SELECT * FROM automobileparts";

    require_once(
    "dbconnect.php");



            
    echo 
    "<table width='100%' border='1'>";
    $Row $QueryResult->fetch_row();
    do {
                 echo 
    "<tr><td>{$Row[0]}</td>";
            
            echo 
    "<td align='right'>{$Row[1]}</td>";
            echo 
    "<td align='right'>{$Row[2]}</td>";
            echo 
    "<td align='right'>{$Row[3]}</td>";
            echo 
    "<td align='right'>{$Row[4]}</td>";
            echo 
    "<td align='right'>{$Row[5]}</td>"
            echo 
    "<td align='right'><img src=\"$Row[6]\"alt=\"\"/></td>"
            echo 
    "<td align='right'><a href=\"Mail2.php?id={$Row[0]}\">Order</a></td>";   
                           
                       echo 
    "</tr>";
            
    $Row $QueryResult->fetch_row();
    } while (
    $Row);
    echo 
    "</table>";



    $DBConnect->close();


    ?>
     
    Last edited: Jun 17, 2009
  2. garrettroyce

    garrettroyce Community Support Community Support

    Messages:
    5,601
    Likes Received:
    239
    Trophy Points:
    63
    You have to use the header() function to set the content-type as image/[your image type]

    Since you want to use the image in a page that has more than just an image, you have to actually use two different scripts. Then, you will access your images like this:

    <img src="get_image.php?image=$image_number" />

    $image_number should be a unique id for your image. If your table isn't set up this way, add a row, integer type, unsigned, auto increment, primary key.

    Then, getting the image is as easy as this: http://bytes.com/groups/php/1655-php-mysql-image-blobs#post4918
     
  3. acellec

    acellec New Member

    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    it doest display can you check my code

    PHP:
    <?

    $TableName "automobileparts";
    $SQLstring "SELECT * FROM automobileparts";

    require_once(
    "dbconnect.php");



    header ("Content-type: image/jpeg");        
    echo 
    "<table width='100%' border='1'>";
    $Row $QueryResult->fetch_row();
    do {
                     
            echo 
    "<tr><td>{$Row[0]}</td>";
            echo 
    "<td align='right'>{$Row[1]}</td>";
            echo 
    "<td align='right'>{$Row[2]}</td>";
            echo 
    "<td align='right'>{$Row[3]}</td>";
            echo 
    "<td align='right'>{$Row[4]}</td>";
            echo 
    "<td align='right'>{$Row[5]}</td>";
            echo 
    "<td align='right'><img src="get_image.php?image=$Row[1]" /></td>";
            echo 
    "<td align='right'></td>"
            echo 
    "<td align='right'><a href=\"Mail2.php?id={$Row[0]}\">Order</a></td>";   
                           
                       echo 
    "</tr>";
            
    $Row $QueryResult->fetch_row();
    } while (
    $Row);
    echo 
    "</table>";



    $DBConnect->close();


    ?>
     
  4. Twinkie

    Twinkie Banned

    Messages:
    1,389
    Likes Received:
    12
    Trophy Points:
    0
    "header ("Content-type: image/jpeg");" should not be in the script printing the html, it should be in the script printing the image. Setting the header as an image ensures that the image data printed in get_image.php displays as an image rather than a bunch of incomprehensible characters.
     
    Last edited: Jun 17, 2009
  5. acellec

    acellec New Member

    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0

    is it like this then ? still doesnt display..can you show me please

    PHP:
    echo "<table width='100%' border='1'>";
    $Row $QueryResult->fetch_row();
    do {
                     
            echo 
    "<tr><td>{$Row[0]}</td>";
            echo 
    "<td align='right'>{$Row[1]}</td>";
            echo 
    "<td align='right'>{$Row[2]}</td>";
            echo 
    "<td align='right'>{$Row[3]}</td>";
            echo 
    "<td align='right'>{$Row[4]}</td>";
            echo 
    "<td align='right'>{$Row[5]}</td>";
            
            
    header("Content-type: image/jpeg");
            print 
    $Row[6];
            echo 
    "<td align='right'></td>"
            echo 
    "<td align='right'><a href=\"Mail2.php?id={$Row[0]}\">Order</a></td>";   
                           
                       echo 
    "</tr>";
            
    $Row $QueryResult->fetch_row();
    } while (
    $Row);
    echo 
    "</table>";
     
    Last edited: Jun 18, 2009
  6. xav0989

    xav0989 Community Public Relation Community Support

    Messages:
    4,467
    Likes Received:
    95
    Trophy Points:
    0
    no, what you wrote will not work, since you a outputing both an image and some html. What Twikie meant was that the header('content-type: image/jpeg') should be in the get_image.php file. Also, instead of doing a do...while loop, you should do a while loop. Way better, if, for example, there are no results. And it will save you a couple lines. Oh and you should seriously consider using names instead of numerical indices. It's way clearer.
    PHP:
    <?php
    //display car info.php

    $TableName "automobileparts";
    $SQLstring "SELECT * FROM automobileparts";

    require_once(
    "dbconnect.php");

         
    echo 
    "<table width='100%' border='1'>";
    while (
    $Row$QueryResult->fetch_row()) {
                     
            echo 
    "<tr><td>{$Row[0]}</td>";
            echo 
    "<td align='right'>{$Row[1]}</td>";
            echo 
    "<td align='right'>{$Row[2]}</td>";
            echo 
    "<td align='right'>{$Row[3]}</td>";
            echo 
    "<td align='right'>{$Row[4]}</td>";
            echo 
    "<td align='right'>{$Row[5]}</td>";
            echo 
    "<td align='right'><img src=\"get_image.php?image={$Row[1]}\" /></td>";
            echo 
    "<td align='right'></td>"
            echo 
    "<td align='right'><a href=\"Mail2.php?id={$Row[0]}\">Order</a></td>";             
            echo 
    "</tr>";
    }

    echo 
    "</table>";

    $DBConnect->close();
    ?> 
    Next, in the get_image.php file:
    PHP:
    <?php
    //get_image.php

    // you need to sanitize it yourself
    $image_id $_GET['image'];

    $TableName "automobileparts";
    $SQLstring "SELECT * FROM automobileparts WHERE id = $image_id LIMIT 1";

    require_once(
    "dbconnect.php");

    header('Content-Type: image/jpeg');
    $Row$QueryResult->fetch_row()
    echo 
    $Row[6];
    $DBConnect->close();
    ?> 
     
  7. acellec

    acellec New Member

    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0

    from the car info.php i just change this part to {$Row[0]}...since it's the part number id for the pictures

    PHP:
    echo "<td align='right'><img src=\"get_image.php?image={$Row[1]}\" /></td>";
    and from the get_image.php

    i change this part id to PartNo.

    PHP:
    $SQLstring "SELECT * FROM automobileparts WHERE id = $image_id LIMIT 1";
    i dont have errors but the picture still doesnt display..can you help me?
    Edit:
    myabe something wrong when i add a field image in mysql? what do you think?
     
    Last edited: Jun 19, 2009
  8. Twinkie

    Twinkie Banned

    Messages:
    1,389
    Likes Received:
    12
    Trophy Points:
    0
    What displays when you follow the direct link to the php image file?
     
  9. acellec

    acellec New Member

    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    it displays characters please help
     
  10. xav0989

    xav0989 Community Public Relation Community Support

    Messages:
    4,467
    Likes Received:
    95
    Trophy Points:
    0
    what kind of characters. There are loads of characters. Copy them here or give us some link that we can chew on...
     
  11. misson

    misson Community Paragon Community Support

    Messages:
    2,572
    Likes Received:
    72
    Trophy Points:
    48
    Even better, post a URL so the forum character encoding won't munge the data and we can examine the HTTP headers. Also, read a tutorial on storing and serving images from MySQL to get a better understanding of what's going on.

    Did you follow xav0989's comment and sanitize $_GET['image'] with (e.g.) filter_var or mysql_escape_string? You don't want to suffer the consequences if you don't.
     
    Last edited: Jun 20, 2009
  12. acellec

    acellec New Member

    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    can you show me how to sanitize?..

    here is my link..

    http://acemysql.freetzi.com/IndexProject.php
     
    Last edited: Jun 21, 2009
  13. acellec

    acellec New Member

    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    attached is my sql fields and how i uploaded my images.. help me please
     

    Attached Files:

  14. misson

    misson Community Paragon Community Support

    Messages:
    2,572
    Likes Received:
    72
    Trophy Points:
    48
    There are plenty of forum posts, tutorials and examples in the PHP documentation that cover sanitization. Go over those and update your code. Alternatively, use prepared statements (with e.g. mysqli::prepare). If you're having problems, read "How To Ask Questions The Smart Way" and follow its guidelines to start a new thread.

    You should also read over some articles on SQL Injection (2 3) so you understand why you're sanitizing input.

    IndexProject.php is still embedding binary image data in the HTML output. Dumping image data into the HTML output of a script will always fail. HTML is a text only format. Apply xav0989's fix of printing an <img> element whose src attribute points to a page that prints the image. I was thus expecting you to post the URL of the image printing page, something like:
    The above gives a parse error:
    It looks like xav0989's get_image.php had a missing semicolon on the "fetch_row()" line. It should read:
    PHP:
    $Row$QueryResult->fetch_row();
     
    Last edited: Jun 21, 2009
  15. xav0989

    xav0989 Community Public Relation Community Support

    Messages:
    4,467
    Likes Received:
    95
    Trophy Points:
    0
    Oops, my bad... should have read over my code... :biggrin:
     
  16. misson

    misson Community Paragon Community Support

    Messages:
    2,572
    Likes Received:
    72
    Trophy Points:
    48
    At this point, whenever I think "This is a short script, I'll just post it," I force myself to test it. Invariably there's a typo.
     
  17. xav0989

    xav0989 Community Public Relation Community Support

    Messages:
    4,467
    Likes Received:
    95
    Trophy Points:
    0
    I should start doing it, as well.

    Oh BTW, mission, can I quote your signature? I find it very to the point nowadays...
     
    Last edited: Jun 21, 2009
  18. acellec

    acellec New Member

    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Last edited: Jun 21, 2009
  19. xav0989

    xav0989 Community Public Relation Community Support

    Messages:
    4,467
    Likes Received:
    95
    Trophy Points:
    0
    Now it justs echo's the file name... Could you post the content of the get_image.php script here again?

    I made a few changes to my script:
    PHP:
    <?php
    //get_image.php

    // you need to sanitize it yourself
    $image_id $_GET['image'];

    $TableName "automobileparts";
    $SQLstring "SELECT * FROM automobileparts WHERE PartNo = $image_id LIMIT 1";

    require_once(
    "dbconnect.php");

    header('Content-Type: image/jpeg');
    $Row $QueryResult->fetch_row();
    echo 
    $Row[5];
    $DBConnect->close();
    ?>
    If you can change the fetch_row() function to use names instead of numbers, it would be great.
     
    Last edited: Jun 21, 2009
  20. acellec

    acellec New Member

    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    here is the code:

    PHP:
    <?php
    //get_image.php

    // you need to sanitize it yourself
    $image_id $_GET['image'];

    $TableName "automobileparts";
    $SQLstring "SELECT * FROM automobileparts WHERE PartNo = '$image_id'";

    require_once(
    "dbconnect.php");

    header('Content-Type: image/jpeg');
    $Row$QueryResult->fetch_row();
    echo 
    $Row[6]; 
    //$Row[6] is $Row['bmkimage'] is the image itself

    $DBConnect->close();
    ?>
     

Share This Page