+ Reply to Thread
Page 1 of 3 123 LastLast
Results 1 to 10 of 28
Like Tree2Likes

Thread: Display image in MYSQL

  1. #1
    acellec is offline x10Hosting Member acellec is an unknown quantity at this point
    Join Date
    May 2009
    Posts
    29

    Display image in MYSQL

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

    $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 by acellec; 06-17-2009 at 03:36 AM.

  2. #2
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: Display image in MYSQL

    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...blobs#post4918
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  3. #3
    acellec is offline x10Hosting Member acellec is an unknown quantity at this point
    Join Date
    May 2009
    Posts
    29

    Re: Display image in MYSQL

    Quote Originally Posted by garrettroyce View Post
    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...blobs#post4918
    it doest display can you check my code

    PHP Code:
    <?

    $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. #4
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: Display image in MYSQL

    "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 by Twinkie; 06-17-2009 at 01:03 PM.
    karimirt47 likes this.

  5. #5
    acellec is offline x10Hosting Member acellec is an unknown quantity at this point
    Join Date
    May 2009
    Posts
    29

    Re: Display image in MYSQL

    Quote Originally Posted by Twinkie View Post
    "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.

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

    PHP Code:
    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 by acellec; 06-18-2009 at 07:56 AM.

  6. #6
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Display image in MYSQL

    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 Code:
    <?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 Code:
    <?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();
    ?>
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  7. #7
    acellec is offline x10Hosting Member acellec is an unknown quantity at this point
    Join Date
    May 2009
    Posts
    29

    Re: Display image in MYSQL

    Quote Originally Posted by xav0989 View Post
    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 Code:
    <?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 Code:
    <?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();
    ?>

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

    PHP Code:
    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 Code:
    $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 by acellec; 06-19-2009 at 11:40 AM. Reason: Automerged Doublepost
    dinomirt96 likes this.

  8. #8
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: Display image in MYSQL

    What displays when you follow the direct link to the php image file?

  9. #9
    acellec is offline x10Hosting Member acellec is an unknown quantity at this point
    Join Date
    May 2009
    Posts
    29

    Re: Display image in MYSQL

    Quote Originally Posted by Twinkie View Post
    What displays when you follow the direct link to the php image file?
    it displays characters please help

  10. #10
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Display image in MYSQL

    what kind of characters. There are loads of characters. Copy them here or give us some link that we can chew on...
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

+ Reply to Thread
Page 1 of 3 123 LastLast

Similar Threads

  1. Which is better postgresql or MySQL?
    By Kurai Kumo in forum Scripts & 3rd Party Apps
    Replies: 11
    Last Post: 12-10-2009, 08:17 AM
  2. Replies: 14
    Last Post: 09-29-2008, 07:07 PM
  3. Connection to x10 Hosted MySQL
    By medilus in forum Free Hosting
    Replies: 1
    Last Post: 09-01-2008, 01:35 AM
  4. WP mysql error : Error establishing a database connection
    By orangpelupa in forum Free Hosting
    Replies: 2
    Last Post: 12-19-2007, 05:08 AM
  5. MySQL Help
    By AsPeRiTy in forum Free Hosting
    Replies: 2
    Last Post: 08-07-2005, 12:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers