The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End How To Display Stored Image Using Path? Reply To: How To Display Stored Image Using Path?


In order to make each record unique I should use an auto incrementing id and store it as a session as well as the username?

The primary key must be unique per record, but that doesn’t mean it has to be an auto-incrementing id. Any unique value will work. If there is already some column that is guaranteed to be unique for all records (the card title, maybe?), then you don’t have to bother with an “artifical” PK. It won’t hurt, though, so if using an auto-inc column makes the most sense to you, go ahead.

I don’t see any reason to store the card’s PK in the session, however. Maybe, if you were displaying a list of submitted cards on a subsequent page? So, the answer here is, “do you need it for some reason?”

I can only display the images inside a query

I’m not sure what you mean by that. In the image you posted, “the query” is your variable $Query. You can’t “do” anything inside it… In fact, the query isn’t even executed by your php script: it’s executed by the database.

Your example displays the query results in a while loop. If you want to use the retrieved values outside of that loop, then you need to store them in a variable. Otherwise, each iteration of the loop overwrites the previous value, so after the loop is done, you only have the last set of values.

The typical solution is to store the values in an array, for example:

//  . . .

// fetch paths from all rows
while( $row = mysqli_fetch_array( $Result ) ){
// store values in an array
    $img_paths[] = $row["path"];

// later . . .

foreach( $img_paths as $img_path ){
    print "<img src='$img_path'>";