fetch multiple image from mysql in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • afroz ahmad
    New Member
    • Mar 2013
    • 18

    fetch multiple image from mysql in php

    i am trying to fetch all images which is store in my mysql database this is my code but not able to fetch please help

    Code:
    <?php
    mysql_connect("localhost", "root", "") or die("Can not connect to database: ".mysql_error());
    mysql_select_db('brightweb') or die("Can not select the database: ".mysql_error());
    
    $sql = "select * from portfolio";
            // the result of the query
            $result = mysql_query($sql) or die("Invalid query: "  .mysql_error());
            // Header for the image
            while ($rows = mysql_fetch_array($result))
    				
            header('Content-type: image/jpg');
    		echo $row['image'];  
    
           
    		
    ?>
    Last edited by Rabbit; May 5 '13, 03:47 AM. Reason: Please use code tags when posting code.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    How exactly are you expecting to use that script?

    Normally when you fetch image data from a database, or pass an image file through PHP, then you do something along the lines of this in your HTML
    Code:
    <img src="fetchImage.php?id=1" alt="Image from PHP">
    Which calls the PHP page as an image to get the image data. The PHP script then fetches the image data from the database/file and passes it to the browser. - As far as the browser is concerned, there is absolutely no difference between calling a static JPEG image and calling a PHP script that returns JPEG image data.

    Which brings me back to my original question: How are you going to be using a PHP script that returns data for multiple images? That would be no use in a normal <img> tag, as it would essentially just be read as a corrupted image.

    If you want to build a page that displays all images stored in a MySQL table, you need to do that in two parts: The first part would create a list of <img> elements, one for each image, that queries a second PHP file with the ID of the image that <img> element is supposed to get. The second part would then be that PHP file, which would fetch that single image from the database and pass it's data back to the <img> tag.

    Comment

    • afroz ahmad
      New Member
      • Mar 2013
      • 18

      #3
      i have already tried this one but this code not show any result

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Did you try what I suggested in my last paragraph?

        The one line of HTML I posted won't work with the kind of PHP script you posted. That was the point I was trying to make.

        Comment

        • Exequiel
          Contributor
          • Jul 2012
          • 288

          #5
          Code:
          <?php
          mysql_connect("localhost", "root", "") or die("Can not connect to database: ".mysql_error());
          mysql_select_db('brightweb') or die("Can not select the database: ".mysql_error());
           
          $sql = "select * from portfolio";
                  // the result of the query
                  $result = mysql_query($sql) or die("Invalid query: "  .mysql_error());
                  // Header for the image
                  while ($rows = mysql_fetch_array($result))
          ?>
          <img src="locationoftheimage/<?php echo echo $row['image'].'.jpg'; ?>">
          you can try that one, you need to specify the file extension of your image when displaying it in img tag. and also the location of the image need to specify to display it properly.
          the img tag must be inside of your while loop.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            The $row['image'] in his code, according to his first post, is not the location of a image file, it's the actual image data. What you are suggesting there assumes it's the name or location of a file on the hard-drive.

            Comment

            • afroz ahmad
              New Member
              • Mar 2013
              • 18

              #7
              thank's sir my problem have been solved

              Comment

              Working...