Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Displaying all images in Database table using PHP

Options
  • 16-03-2006 4:53pm
    #1
    Closed Accounts Posts: 252 ✭✭


    Hi guys,

    This is giving me a lot of problems. I am able to insert in images in a table, and display ONE image. But I want to display all the images in the table, this is me giving trouble, this code at the moment is displaying the same image ten times.

    As I am incrementing the $ID variable, I want different images to be displayed, but I just keep getting the same one. I do know it is working to some extent, when I put in this statement(echo $name; ) I do get all the image names.


    CODE

    <?

    $db = mysql_connect("******", "*****","*****");

    mysql_select_db("****",$db);

    $id = 1;
    while($id<10){
    $query = "SELECT name, type, size, content FROM upload where id ='$id'";
    $result = mysql_query($query) or die('Error, query failed');
    list($name, $type, $size, $content) = mysql_fetch_array($result);


    if ($_REQUEST[gim] == $id) {


    header("Content-length: $size");
    header("Content-type: $type");


    echo $content;

    exit;
    }






    ?>

    <html>
    <title>Upload an image to a database</title><body>

    <center><img src=?gim=<? echo $id ?> width=160 height=160><br>


    </body>
    </html>

    <?
    $id++;
    }
    ?>



    _______________________________
    Edit - I had pasted in the wrong version of the code, which was calling 1 as the value, rather than the incremented $id............this version seems to be calling the correct image address(gim=2 or gim=3 and so on) but isn't displaying the relevant image


Comments

  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    Well, for a start, don't store images in a table unless you've got a very good reason for it.


  • Registered Users Posts: 249 ✭✭frost


    You aren't incrementing $id in your loop.


  • Registered Users Posts: 919 ✭✭✭timeout


    You aren't incrementing $id in your loop.
    Well its in it now at the end.

    This is your problem
    where id ='$id'";
    should be
    where id=" . $id;
    
    OR
    where id='" . $id . "'";
    
    if you need the single quotes.


  • Registered Users Posts: 249 ✭✭frost


    timeout wrote:
    Well its in it now at the end.
    Oops, missed that at the end!
    timeout wrote:
    This is your problem

    should be
    where id=" . $id;
    
    OR
    where id='" . $id . "'";
    
    if you need the single quotes.

    Should also work with curly braces:
    $query = "SELECT name, type, size, content FROM upload where id ='{$id}'";
    


Advertisement