How to display all the images stored inside a database

29,326

From what i understand from your post is that uploading and storing isn't a problem, but showing the images is. That's probably because you're using vars that are not set, so no results kan be found in the database. If i misunderstood let me know.

<?php
// No ID
$image = mysql_query("SELECT * FROM images ORDER BY id DESC");   
?>

Also look at what Prof83 says. Ignore my post if your script works with just one image.

Last but not least, if you're using different filetypes, also echo the correct MIME format in the header.

Update I combined both answers.

Edit your loop:

<?php
while($row = mysql_fetch_assoc($image))
{
        echo '<img src="img.php?id='.$row["id"].'">';
}
?>

Create a page name img.php

<?php
$query = mysql_query("SELECT image FROM images WHERE id = ".$_GET['id']);
$row = mysql_fetch_assoc($query);
header("Content-type: image/jpeg");
echo $row['image'];
?>
Share:
29,326
SimonCode
Author by

SimonCode

Think before you ASK! :)

Updated on March 19, 2020

Comments

  • SimonCode
    SimonCode about 4 years

    I am making a gallery that uses a MySQL database (yeah I know it's a bad practice but it's the requirement for the moment.) I can upload multiple images but I'm having trouble displaying all images stored inside the database. The FORM allows five images to be uploaded. Then the user must proceed to another page where all the images in database (including the ones uploaded recently) will be displayed together with the description of the images. I have code already but the one that will work on the display is not working or I think is wrong.

    Here is the form code:

     <html>
     <head>
        <title> Upload image</title>
    
     </head>
     <body> 
     <div align="center">
        <form action="fUpload.php" method="POST" enctype="multipart/form-data">
        All forms must be filled. <br />
        File: <br />
        <input type="file" name="image[]"/> <input type="text" name="imageDescription[]" size="30" /> <br />
        <input type="file" name="image[]"/>  <input type="text" name="imageDescription[]" size="30" /> <br />
        <input type="file" name="image[]"/>  <input type="text" name="imageDescription[]" size="30" /> <br />
        <input type="file" name="image[]"/>  <input type="text" name="imageDescription[]" size="30" /> <br />
        <input type="file" name="image[]"/> <input type="text" name="imageDescription[]" size="30" /> <br />
    
        <input type="submit" value="Upload image" />
    
        </form>
    </div>  
    </body>
    </html>
    

    Here is the script that would upload:

     <?php 
    
    //connect to the database//
    $con = mysql_connect("localhost","root", "");
    if(!$con)
    {
     die('Could not connect to the database:' . mysql_error());
     echo "ERROR IN CONNECTION";
    }
    
    $sel = mysql_select_db("imagedatabase");
    if(!$sel)
    {
     die('Could not connect to the database:' . mysql_error());
     echo "ERROR IN CONNECTION";
    }
    //file properties//
    
    $file = $_FILES['image']['tmp_name']; 
    
    echo '<br />';
    
     /*if(!isset($file))
        echo "Please select your images";
    
    else
    {
     */for($count = 0; $count < count($_FILES['image']); $count++)
    {
    //$image = file_get_contents($_FILES['image']['tmp_name']);
        $image_desc[$count] = addslashes($_POST['imageDescription'][$count]);
        $image_name[$count] = addslashes($_FILES['image]']['name'][$count]); echo '<br \>';
        $image_size[$count] = @getimagesize($_FILES['image']['tmp_name'][$count]);
        $error[$count] = $_FILES['image']['error'][$count];
    
        if($image_size[$count] === FALSE  || ($image_size[$count]) == 0)
            echo "That's not an image";
        else
        {
    
        // Temporary file name stored on the server
         $tmpName[$count]  = $_FILES['image']['tmp_name'][$count];
    
      // Read the file
        $fp[$count]   = fopen($tmpName[$count], 'r');
        $data[$count] = fread($fp[$count], filesize($tmpName[$count]));
        $data[$count] = addslashes($data[$count]);
         fclose($fp[$count]);
    
    
      // Create the query and insert
      // into our database.
    
      $results = mysql_query("INSERT INTO images( description, image) VALUES             ('$image_desc[$count]','$data[$count]')", $con);
    
            if(!$results)
            echo "Problem uploding the image. Please check your database";  
        //else 
        //{
            echo "";
            //$last_id = mysql_insert_id();
            //echo "Image Uploaded. <p /> <p /><img src=display.php?    id=$last_id>";
            //header('Lcation: display2.php?id=$last_id');
            }
        //}
    }
    
    
    mysql_close($con);
    header('Location: fGallery.php');
    ?>
    

    And finally the one that should display:

    <html>
    <body>
    
    </body>
    <?php
    
    //connect to the database//
    mysql_connect("localhost","root", "") or die(mysql_error());
    mysql_select_db("imagedatabase") or die(mysql_error());
    
    //requesting image id
    
    $id = addslashes($_REQUEST['id']);
    
    $image = mysql_query("SELECT * FROM images WHERE id = $id");
    
    
    while($datum = mysql_fetch_array($image, MYSQL_ASSOC))
    {
            printf("Description %s $image = $image['image'];
    
    header("Content-type: image/jpeg");
    
    }
    
    mysql_close();
    
    
    ?>
    

    Your help is much appreciated. I need it badly to move on.

  • SimonCode
    SimonCode over 12 years
    Thanks for that hint. :D I'll let you know if it worked., Thanks again.. :D
  • SimonCode
    SimonCode over 12 years
    I got another error.. it says Warning: Cannot modify header information - headers already sent by (output started at C:\XAMP\xampp\htdocs\gallery\fUpload.php:21) in C:\XAMP\xampp\htdocs\gallery\fUpload.php on line 70 and no display comes out.. what's wrong?
  • SimonCode
    SimonCode over 12 years
    wow! thanks! I never realized that.. thanks for the information.. where exactly should I place the php code with foreach? :D
  • Prof
    Prof over 12 years
    inside your display page, the page to display all the images... PS. if this helped you, please mark it as the correct answer :D
  • Prof
    Prof over 12 years
    I have amended the answer to help understand the foreach statement
  • SimonCode
    SimonCode over 12 years
    can I be very direct? Can you do it for me? I mean can you correct the code for the gallery? Im already confused.. Ive been working on this since yesterday.. :(
  • SimonCode
    SimonCode over 12 years
    So i wont use the while or I'll still use it? Im getting more confused.. ahha sorry..
  • SimonCode
    SimonCode over 12 years
    Im sorry i cant get it easily but for the foreach should I put it inside the while? or its the only one inside the gallery process with the database connection script? please help me understand. New to this.
  • mat
    mat over 12 years
    @SimonCode i updated my answer (didn't want to mess with Prof83's answer). I think that should work.
  • SimonCode
    SimonCode over 12 years
    @mat thanks mat. I'll try to use that.. anyway.. Im having another error.. with the header on the upload process.. T_T what's happening to this code? :(
  • SimonCode
    SimonCode over 12 years
    I'm having another problem with the header thing.. it keeps coming out..the warning..
  • SimonCode
    SimonCode over 12 years
    it is displaying already but only corrupted images.. :( something is still wrong..im figuring it out.. please help .. :D
  • mat
    mat over 12 years
    A few questions: Do you still have the header problem? Are the files jpegs? Could you turn on error reporting and also show warnings? Are the images correctly added to the database?
  • SimonCode
    SimonCode over 12 years
    I already got it.. aha.. sorry Im too stupid to forget to include the database connection.. THANK YOU SO MUCH @mat !! you are a great help.. :D
  • mat
    mat over 12 years
    No problemen Simoncode! Could you please upvote my answer? Thanks
  • SimonCode
    SimonCode over 12 years
    I just did.. tell me if I did it right.. there might be some errors.. some gets unaaccepted or downvotes.. hehe.. better check
  • mat
    mat over 12 years
    Not sure its allieer, but i did ;)
  • Prof
    Prof over 12 years
    Apologies guys, i went AWOL :/ Yeah sorry about the foreach it's the way my DB class returns a result instead of the long way around, i always forget :D Thanks Mat... that's 100%