0

I am having a little trouble trying to display the image at ID: 2 in my store table...

here's what i've come up with (p.s. the reason I set $id = 2 is because I plan on making this a loop where I will increment that value to print other images)

<!DOCTYPE html> <html> <head> <title>Gallery</title> </head> <body> <?php mysql_connect ... mysql_select_db ... $id = 2; $image = mysql_query("SELECT * FROM store WHERE id=$id"); $image = mysql_fetch_assoc($image); $image = $image['image']; echo $image; ?> </body> </html> 
5
  • 1
    "the reason I set $id = 2 is because I plan on making this a loop where I will increment that value to print other images)" You mean you are going to query the database in a loop -> bad idea. Commented Nov 4, 2012 at 0:43
  • 1
    Please don't use mysql_* functions in new code. They were removed from PHP 7.0.0 in 2015. Instead, use prepared statements via PDO or MySQLi. See Why shouldn't I use mysql_* functions in PHP? for more information. Commented Nov 4, 2012 at 0:43
  • Side Note: Why SELECT * if you only use image? Just SELECT image. The rest of the columns are just wasted resources. Commented Nov 4, 2012 at 0:49
  • this as it is does nothing, i'm going to change this so that it store the location to SQL which I found will make this wildly easier/possible... I will also check those links out, I am using an older book atm and that's probably why i'm a bit outdated Commented Nov 4, 2012 at 0:53
  • David, I caught that too, just a mistake it was meant to be image Commented Nov 4, 2012 at 1:00

2 Answers 2

2

First, some caveats:

  • Don't use the mysql_* functions
  • Don't store images in SQL. Store the location. (You may already be doing this).

If you're storing the location, you'll need to add <img> tags to the echo

echo "<img src='$image'>"; 
Sign up to request clarification or add additional context in comments.

1 Comment

Full SQL message: Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
0

Maybe you need to echo the markup also?

echo '<img src="'.$image.'">'; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.