how to display image from database?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nse111
    New Member
    • Jul 2008
    • 21

    #46
    OMG!! I'v bin lookin for this for sooooooooooo long! all of u r life savers!!!!! thaaaaaaaaaaaaa aaaaaaaanks!!! I'm a newbie myself :( thanks thanks... :)

    Comment

    • vinco83
      New Member
      • Jan 2009
      • 1

      #47
      Please help!

      Hey everyone...

      I've tried following this thread, and can't seem to get the code to work! I've been working on this for hours now, and i'm going crazy here lol

      When I go to test2.php (which should show me the images), all it shows are image placeholders! It's pulling the images somewhat correctly, if I upload another image to the table...it'll show another placeholder, and viewing properties on the images shows the correct URL...the image just isn't showing up at all :( Can anyone help?

      Here's my code:

      test.php
      Code:
       <?php 
          $errmsg = "";
          if (! @mysql_connect("localhost","root","welcome"))
          {
          $errmsg = "Cannot connect to database";
          }
          @mysql_select_db("emotion");
           
          if (IsSet($_GET['image_id'])){
         $gotten = @mysql_query("select image, type from whats_hot where image_id = ".$_GET['image_id']);
         list( $type,$data ) = mysql_fetch_row( $query );
         header("Content-type: {$type}");
         while ($row = mysql_fetch_array($gotten,MYSQL_ASSOC))
         {
         print $row['image'];
          
         }
         mysql_free_result($gotten);
         }
         ?>
      test2.php
      Code:
         <?php
         $errmsg = "";
         if (! @mysql_connect("localhost","root","welcome"))
         {
         $errmsg = "Cannot connect to database";
         }
         @mysql_select_db("emotion");
          
         $strSQL = "select * from whats_hot";
         $rsPix = mysql_query($strSQL);
         $numRows = mysql_numrows($rsPix);
         $i = 0;
          
         while($i < $numRows){
         ?>
         <img src="test.php?image_id=<?php echo mysql_result($rsPix,$i,"image_id"); ?>"/>
         <?php
         $i++;
         }
         ?>
      my whats_hot table is set up like so:

      image_id (int) auto_increment
      image (MEDIUMBLOB)
      type (varchar)

      Comment

      • tharden3
        Contributor
        • Jul 2008
        • 916

        #48
        Originally posted by harshmaul
        So you need two pages.

        The first page uses content disposition to output an image given an ID in the quesry string just like you have already only modify the query to select an image based on the query string. Like this.....


        Call this page pix.php...

        Code:
        <?php 
        $errmsg = "";
        if (! @mysql_connect("localhost","root",""))
        {
        $errmsg = "Cannot connect to database";
        }
        @mysql_select_db("upload");
        
        if (IsSet($_GET['pixID'])){
        $gotten = @mysql_query("select imgdata from pix where pixID = ".$_GET['pixID']);
        header("Content-type: image/jpeg");
        while ($row = mysql_fetch_array($gotten))
        {
        print $row['imgdata'];
        
        }
        mysql_free_result($gotten);
        }
        ?>
        the other page will have a list of img tags with the source pointing to a variation of pix.php and a query string value.


        call this page list.php


        Code:
        <?php
        $errmsg = "";
        if (! @mysql_connect("localhost","root",""))
        {
        $errmsg = "Cannot connect to database";
        }
        @mysql_select_db("upload");
        
        $strSQL = "select * from pix";
        $rsPix = mysql_query($strSQL);
        $numRows = mysql_numrows($rsPix);
        $i = 0;
        
        while($i < $numRows){
        ?>
        <img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/>
        <?php
        $i++;
        }
        ?>
        Now put these into the same folder and it should work. i haven't tested you may need to debug slightly
        This page has a lot of great information, and I'm glad Bytes has such active and helpful members. I'm a bit confused though. I was wondering if you could help me out.

        I'm trying to achieve basically the same thing that these two pages (pix.php and list.php) seek to create. I'd like to take the images from a database, and show those images to my website users in a nice neat little column. You don't need to show me any code, you've already done that. I just have a few questions.

        So the first thing I need to do is set up my tables with a BLOB field labeled imgdata, and a pixID field labeled..... well, what exactly is the pixID field, and how should it be labeled? What are the signifigance of each of these pages? Which part of the code am I just referencing to, and which part actually shows the image? I'd just like a little clarification. I'm awfully new to this. Thanks Bytes.

        -Tim

        Comment

        • naimlatifi
          New Member
          • Feb 2012
          • 1

          #49
          Hi everyone,

          I tried to make this to work but I am having the same problem as vinco83. The images seems to get retrived correctly but it only shows the placholder not image. when I debugged it says that "ID not found in Msyql result index 5"

          Looking forward to hear from you soon !


          Best regards,
          Naim

          Comment

          Working...