problem in displaying multiple images

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • swethak
    New Member
    • May 2008
    • 118

    problem in displaying multiple images

    Hi,.

    when i display the image by using below code i got the error as image displayed in the form of garbage value format.plz tell that what's the mistake in my code.

    [PHP]
    <?php
    mysql_connect(" localhost", "root", "root") or die(mysql_error ());
    mysql_select_db ("rainlist") ;
    $result = mysql_query(spr intf("SELECT * from pix WHERE pid = 1", $_GET['pid']));
    echo "SELECT * from images WHERE pid=".$_GET['id'];
    $row = mysql_fetch_arr ay($result);
    header("Content-type: image/jpeg");
    $image=$row['imgdata'];

    echo $image;
    ?>
    [/PHP]
  • swethak
    New Member
    • May 2008
    • 118

    #2
    problem in displaying multiple images

    hi,

    i wrote a code to display multiple images.But it display only first image.plz tell that what's the problem in my code.

    [code=php]
    <?php
    header("Content-type: image/jpeg");
    if (! mysql_connect(" localhost","roo t","root")) {
    $errmsg = "Cannot connect to database";
    }
    mysql_select_db ("rainlist") ;
    $gotten = mysql_query("se lect * from pix");
    while($row = mysql_fetch_arr ay($gotten)) {
    echo $row['imgdata'];
    }
    ?>
    [/code]
    Last edited by Atli; Jun 11 '08, 10:31 PM. Reason: Added [code] tags.

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      I've never had this problem, as I've never been in this situation, but my guess is: because you're using Content-type: image/jpeg you'll only be able to display one image.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        You've posted this again

        Don't double post.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by markusn00b
          You've posted this again

          Don't double post.
          The two posts are indeed pretty much the same. As I see it, they describe two different attempts to fix the same problem.
          I have merged them into this one.

          Please try to keep all posts regarding the same problem in the same thread. Even if your attempts to fix the original problem are causing completely different errors, just post them in the same thread so it doesn't get scattered all over the place.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            The reason why the code in the first post is generating garbage data is that you are printing data before you tell the browser what type of data to expect (that is, you are printing the query before you set the content-type and print the data). When you do this, the browser will automatically expect HTML data, which will display your image data as random hex characters.

            For example, this will cause the output you described:
            [code=php]
            echo "Hello!";
            header("Content-Type: image/jpeg");
            readfile("somei mage.jpg");
            [/code]
            While this, would show an image:
            [code=php]
            //echo "Hello!";
            header("Content-Type: image/jpeg");
            readfile("somei mage.jpg");
            [/code]
            Note, that the only change is that I commented out the echo statement.

            Comment

            Working...