how to display image from database?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mirianCalin
    New Member
    • Feb 2008
    • 35

    how to display image from database?

    i am doing a site for appliance center..
    i need to display all the products that the company offers, but my problem is that i cant display ALL the images in my database.. the first entry on the database is the only one that displays..
    i am using mysql as database

    here's the code.. tell me what's my error and pls. kindly edit it..

    **imgdata = it is where i store the image.. longblob is the data type
    [code=php]
    <?php
    $errmsg = "";
    if (! @mysql_connect( "localhost","ro ot",""))
    {
    $errmsg = "Cannot connect to database";
    }
    @mysql_select_d b("upload");

    $gotten = @mysql_query("s elect imgdata from pix");
    header("Content-type: image/jpeg");
    while ($row = mysql_fetch_arr ay($gotten))
    {
    print $row['imgdata'];

    }
    mysql_free_resu lt($gotten);
    ?>[/code]
    Last edited by pbmods; Jan 17 '09, 12:43 AM. Reason: Added CODE tags.
  • sandeepsandeep
    New Member
    • Dec 2006
    • 50

    #2
    hi
    change in the mysql_fetch_arr ay()
    [code=php]
    <?php
    $errmsg = "";
    if (! @mysql_connect( "localhost","ro ot",""))
    {
    $errmsg = "Cannot connect to database";
    }
    @mysql_select_d b("upload");

    $gotten = @mysql_query("s elect imgdata from pix");
    header("Content-type: image/jpeg");

    while ($row = mysql_fetch_arr ay($gotten,MYSQ L_ASSOC))[/code]
    Last edited by pbmods; Jan 17 '09, 12:43 AM. Reason: Added CODE tags.

    Comment

    • harshmaul
      Recognized Expert Contributor
      • Jul 2007
      • 490

      #3
      Hi,
      the problem here is that you are only able to use the content disposition to send out one file.
      As each blob stores a different image you will need another page thats loops through the DB, and displays the content disposition page.

      Steps to achieve this....
      Firstly change your select query to be based on the ID in the table, and get that ID from the query string ($_GET['ID']) or something like that.

      Secondly make another page and in that page loop through the results from the table printing out an image with the content disposition page and an ID in the query string.

      I can go in to more detail if you like. let me know

      Comment

      • mirianCalin
        New Member
        • Feb 2008
        • 35

        #4
        Originally posted by harshmaul
        Hi,
        the problem here is that you are only able to use the content disposition to send out one file.
        As each blob stores a different image you will need another page thats loops through the DB, and displays the content disposition page.

        Steps to achieve this....
        Firstly change your select query to be based on the ID in the table, and get that ID from the query string ($_GET['ID']) or something like that.

        Secondly make another page and in that page loop through the results from the table printing out an image with the content disposition page and an ID in the query string.

        I can go in to more detail if you like. let me know
        yes, please help me..
        and can you show me the code?
        im really new with php and im having a hard time..
        thanks for the reply..
        very much appreciated

        Comment

        • mirianCalin
          New Member
          • Feb 2008
          • 35

          #5
          Originally posted by sandeepsandeep
          hi
          change in the mysql_fetch_arr ay()
          [code=php]
          <?php
          $errmsg = "";
          if (! @mysql_connect( "localhost","ro ot",""))
          {
          $errmsg = "Cannot connect to database";
          }
          @mysql_select_d b("upload");

          $gotten = @mysql_query("s elect imgdata from pix");
          header("Content-type: image/jpeg");

          while ($row = mysql_fetch_arr ay($gotten,MYSQ L_ASSOC))[/code]
          hi!! thanks for the reply..
          very much appreciated..
          i'll try this tomorrow and will inform you about the result..
          thanks again!!
          Last edited by pbmods; Jan 17 '09, 12:44 AM. Reason: Added CODE tags.

          Comment

          • harshmaul
            Recognized Expert Contributor
            • Jul 2007
            • 490

            #6
            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

            Comment

            • mirianCalin
              New Member
              • Feb 2008
              • 35

              #7
              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
              hey, thank you very much!! it works!!
              i hope i can ask you again for further problems.. :-)
              thank you..
              thank you..
              thank you!!

              Comment

              • harshmaul
                Recognized Expert Contributor
                • Jul 2007
                • 490

                #8
                hi,
                absolutly no problem. i love to help. But make sure you ask in the forums so every one can learn.

                Comment

                • mirianCalin
                  New Member
                  • Feb 2008
                  • 35

                  #9
                  Originally posted by harshmaul
                  hi,
                  absolutly no problem. i love to help. But make sure you ask in the forums so every one can learn.
                  i have another problem..
                  i also want to display the corresponding title of the image..
                  "title" is the field that handles the title of the image in my table

                  Comment

                  • harshmaul
                    Recognized Expert Contributor
                    • Jul 2007
                    • 490

                    #10
                    Originally posted by mirianCalin
                    i have another problem..
                    i also want to display the corresponding title of the image..
                    "title" is the field that handles the title of the image in my table
                    Hi again...

                    Something like this..... for the page which displays all the images

                    [code=php]
                    <?php
                    $errmsg = "";
                    if (! @mysql_connect( "localhost","ro ot",""))
                    {
                    $errmsg = "Cannot connect to database";
                    }
                    @mysql_select_d b("upload");

                    $strSQL = "select * from pix";
                    $rsPix = mysql_query($st rSQL);
                    $numRows = mysql_numrows($ rsPix);
                    $i = 0;

                    while($i < $numRows){
                    ?>
                    <h1><?php echo mysql_result($r sPix,$i,"pixTit le"); ?></h1>
                    <img src="pix.php?pi xID=<?php echo mysql_result($r sPix,$i,"pixID" ); ?>"/>
                    <?php
                    $i++;
                    }
                    ?>[/code]
                    Last edited by pbmods; Jan 17 '09, 12:44 AM. Reason: Added CODE tags.

                    Comment

                    • mirianCalin
                      New Member
                      • Feb 2008
                      • 35

                      #11
                      Originally posted by harshmaul
                      Hi again...

                      Something like this..... for the page which displays all the images

                      [code=php]
                      <?php
                      $errmsg = "";
                      if (! @mysql_connect( "localhost","ro ot",""))
                      {
                      $errmsg = "Cannot connect to database";
                      }
                      @mysql_select_d b("upload");

                      $strSQL = "select * from pix";
                      $rsPix = mysql_query($st rSQL);
                      $numRows = mysql_numrows($ rsPix);
                      $i = 0;

                      while($i < $numRows){
                      ?>
                      <h1><?php echo mysql_result($r sPix,$i,"pixTit le"); ?></h1>
                      <img src="pix.php?pi xID=<?php echo mysql_result($r sPix,$i,"pixID" ); ?>"/>
                      <?php
                      $i++;
                      }
                      ?>[/code]
                      hello!! thanks for replying again..
                      you're great.. it worked!!
                      mmm.. can i ask you again?
                      how can i display all the images in a table of 3 columns while the rows will depend on how many images is in my database

                      ex:
                      total # of images: 25
                      so, there will be 3 columns and 9 rows (the last row has only 1 image)

                      thanks again for the reply!!
                      Last edited by pbmods; Jan 17 '09, 12:45 AM. Reason: Added CODE tags.

                      Comment

                      • harshmaul
                        Recognized Expert Contributor
                        • Jul 2007
                        • 490

                        #12
                        Hi,
                        I appreciate your gratitude.

                        I don't get that last question fully.
                        What is the table structure for this new table (you can give me the create query if you like). I can try work it out or give you a proper solution, but i need more info this time...

                        :)

                        Comment

                        • mirianCalin
                          New Member
                          • Feb 2008
                          • 35

                          #13
                          Originally posted by harshmaul
                          Hi,
                          I appreciate your gratitude.

                          I don't get that last question fully.
                          What is the table structure for this new table (you can give me the create query if you like). I can try work it out or give you a proper solution, but i need more info this time...

                          :)
                          ah ok, i'll explain it again..
                          previously, you've given me the code for displaying all the images in my database.. when i tried it, it looks like this ( # for example is the image)
                          # # # # # # # #

                          i tried putting the code inside a table.
                          here's the code:
                          [code=php]
                          <table>
                          <tbody>
                          <?
                          if($colCtr % $cols == 0)
                          {
                          ?>
                          <tr>
                          <td><img src="pix.php?pi d=<?php echo mysql_result($r sPix,$i,"pid"); ?>"/></td>
                          </tr>
                          <? $colCtr++;
                          }
                          ?>
                          </tbody>
                          </table>

                          but the output looks like this:
                          #
                          #
                          #
                          #
                          #
                          #
                          #
                          #
                          [/code]
                          what i want is to be displayed it in 3 columns while the rows will depend on the number of images in my database
                          ex: total number of images is 7
                          Code:
                          display: 
                                    # # #
                                    # # #
                                    #
                          ex: total number of images is 11
                          Code:
                          display:
                                    # # #
                                    # # #
                                    # # #
                                    # #
                          ex: total number of images is 6
                          Code:
                          display:
                                    # # #
                                    # # #
                          please help me again..
                          and thanks for the reply..
                          thank you very much..
                          Last edited by pbmods; Jan 17 '09, 12:45 AM. Reason: Added CODE tags.

                          Comment

                          • harshmaul
                            Recognized Expert Contributor
                            • Jul 2007
                            • 490

                            #14
                            Hi, again!

                            Try using the div solution...
                            (the output code will be alot cleaner).
                            If you definately need tables i have another solution. but i'm not proud of that code... (very messy)

                            Div solution....
                            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;
                            ?>
                            <div>
                            <?php
                            while($i < $numRows){
                            ?>
                            <div style="width:33%: float:left">
                            <img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/>
                            </div>
                            <?php
                            $i++;
                            }
                            ?>
                            </div>

                            Table solution.....

                            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;
                            ?>
                            <table>
                                <tr>
                                    <?php
                                    while($i < $numRows){
                                    ?>
                                        <td>
                                        <img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/>
                                        </td>
                                    <?php
                                        if ($i%3 == 0){
                                    ?>
                                </tr><tr>
                                        <?php
                                        }
                                    ?>
                                    <?php
                                    $i++;
                                    }
                                    ?>
                                    <?php
                                    if ($numRows%3 > 0){
                                    ?>
                                        <td colspan="<?php echo $numRows%3; ?>">&nbsp;</td>
                                    <?php
                                    }
                                    ?>
                                </tr>
                            </table>
                            Again i must say the code may need a spot of debugging.

                            Comment

                            • mirianCalin
                              New Member
                              • Feb 2008
                              • 35

                              #15
                              Originally posted by harshmaul
                              Hi, again!

                              Try using the div solution...
                              (the output code will be alot cleaner).
                              If you definately need tables i have another solution. but i'm not proud of that code... (very messy)

                              Div solution....
                              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;
                              ?>
                              <div>
                              <?php
                              while($i < $numRows){
                              ?>
                              <div style="width:33%: float:left">
                              <img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/>
                              </div>
                              <?php
                              $i++;
                              }
                              ?>
                              </div>

                              Table solution.....

                              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;
                              ?>
                              <table>
                                  <tr>
                                      <?php
                                      while($i < $numRows){
                                      ?>
                                          <td>
                                          <img src="pix.php?pixID=<?php echo mysql_result($rsPix,$i,"pixID"); ?>"/>
                                          </td>
                                      <?php
                                          if ($i%3 == 0){
                                      ?>
                                  </tr><tr>
                                          <?php
                                          }
                                      ?>
                                      <?php
                                      $i++;
                                      }
                                      ?>
                                      <?php
                                      if ($numRows%3 > 0){
                                      ?>
                                          <td colspan="<?php echo $numRows%3; ?>">&nbsp;</td>
                                      <?php
                                      }
                                      ?>
                                  </tr>
                              </table>
                              Again i must say the code may need a spot of debugging.
                              hi.. i've tried the codes you've given me.. the first one still displays the images like this:
                              Code:
                              #
                              #
                              #
                              #
                              #
                              the 2nd one is the one that i need but there's something wrong, the first row has only 1 image but the succeeding rows are fine.. it displays like this:
                              Code:
                              #
                              # # #
                              # # #
                              # # #
                              where's the problem with the code?
                              and where can i put this code

                              <td><center><?p hp echo mysql_result($r sPix,$i,"title" ); ?></center></td>

                              so that the image has a title under it?
                              thanks again for the reply..
                              Last edited by pbmods; Jan 17 '09, 12:46 AM. Reason: Added CODE tags.

                              Comment

                              Working...