Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying an image using a php variable

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

like image 567
user1783150 Avatar asked Oct 24 '13 10:10

user1783150


People also ask

How can we store image in variable in PHP?

Store the image in a variable such that, Echo the image from the variable without changing headers. You can do this in two ways. In way one, you serialize the image in a string, and save the string in the session.

How do I echo an image in PHP?

You cant echo an image using PHP. echo is for strings only. However, you can echo the image source - img src="" Just make sure you put the picture extension at the end of the file you are grabbing.

How display all images from database in PHP?

$con = mysqli_connect('***', '***', '***', '***_dbimage'); if(isset($_GET['id'])) { $id = mysql_real_escape_string($_GET['id']); $query = mysql_query("SELECT * FROM store WHERE id=$id"); while($row = mysql_fetch_assoc($query)) { $imageData = $row['image']; } header("content-type:image/jpeg"); echo $imageData; } else { ...

What is $_ in PHP?

PHP $_GET is a PHP super global variable which is used to collect form data after submitting an HTML form with method="get". $_GET can also collect data sent in the URL.


3 Answers

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'];

like image 60
andreashager Avatar answered Sep 21 '22 06:09

andreashager


Try this

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

like image 21
Shankar Narayana Damodaran Avatar answered Sep 18 '22 06:09

Shankar Narayana Damodaran


try

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

or

<img src= "<? echo $image; ?>" alt="test"/>
like image 45
Taytay Avatar answered Sep 19 '22 06:09

Taytay