5

I'm trying to display images by taking their file path from an sql table, but i'm having a lot of troubles.

Here is whats going on:

$image is a variable containing the text "itemimg/hyuna.png" which is path to an image.

$image = 'itemimg/hyuna.png';

I assumed I would be able to display the image outside of the php block like so:

<img src= "<? $image ?>" alt="test"/>

This doesn't work though for some reason.

So I thought maybe it's not able to read the variable outside the php block(i'm a beginner), so for testing i did:

<h1> "<? $image ?>" </h1>

It displays itemimg/hyuna.png as a h1 banner.

Meaning it's accessing the varible fine.

So I thought maybe the path is wrong. So I tried:

<img src= "itemimg/hyuna.png" alt="test"/>

This displays the image perfectly.

So now I'm stuck scratching my head why the first bit of code displays nothing but the text "test" from "alt="

Extra question: How do I go about assigning a value from an sql cell to a variable? I attempted the following with no luck:

$q = "select * from item where id=$id";
$results = mysql_query($q);
$row = mysql_fetch_array($results, MYSQL_ASSOC);
$image = ".$row['image'].";

item is a table with a collumn: image which contains file paths to images

3
  • 1
    PHP short-tags are probably not enabled. Even still you're using them wrong. If you want to echo, use <?=. Commented Oct 24, 2013 at 10:42
  • 1
    "If you're happy and you know it syntax error" The echo short tag is <?=, not <?. The latter need to be enabled, too, and even if they are, you shouldn't use them, as they conflict with the xml tag (<?xml), and make your code less portable. because you're not using the right tag, php will also complain about the missing semi-colon. And even if you fix that: $someVar; is a valid statement that does nothing. Commented Oct 24, 2013 at 10:45
  • Oh god. So basically I just had to add '=' to fix my entire issue? Thank you both so very much for your help! Commented Oct 24, 2013 at 10:47

4 Answers 4

5

First of all, you should not use PHP Shorttags.

When you use the PHP Shorttags you have to say:

<img src="<?=$image ?>" alt="test" />

But i would encourage to escape the Content off the variable like this:

<img src="<?php echo htmlspecialchars($image); ?>" alt="test" />

Your extra question:

This should lead to an syntax error because the string could not be parsed, just use $image = $row['image'];

Sign up to request clarification or add additional context in comments.

3 Comments

There's nothing wrong with the short-echo-tag <?=. It's always enabled. htmlspecialchars is a good point, though a bit much... I'd suggest just doing str_replace('"','',$image).
Works perfectly. What would be benefit of using the second example over the first?
If you have for example a double quote (") in your string and you just echo the content of the variable the double quoute will ruin the html attribute tag (not proper closed) and invalidate the html.
3

Try this

<img src= "<?php echo $image ?>" alt="test"/>

Comments

1

try

<img src= "<?= $image ?>" alt="test"/>

or

<img src= "<? echo $image; ?>" alt="test"/>

Comments

-2

try this

<img src= "<?php echo $image ?>" alt="test"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.