user3430970
user3430970

Reputation: 13

Displaying images in php error

I am using this code but it is giving me errors. How do I display the images in a table using php?

echo "<td>"."<img src=\"=View.php?image_id=$row['Id']>\""."</td>";

I am getting a syntax error, how can I fix this?

 The error that I get is -  Parse error: syntax error, unexpected 
T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in 
C:\xampp\htdocs\gallery_test\listImages.php on line 45

Thank You

Upvotes: 1

Views: 56

Answers (3)

halfpastfour.am
halfpastfour.am

Reputation: 5933

Let's break it down:

echo "<td>"
 . "<img src=\"=View.php?image_id=$row['Id']>\""
 . "</td>";

First we need to fix the misplaced =. After that, we need to fix the misplaced quote. Finally, wrap $row['Id'] in brackets to fix the syntax error. It should now look like this:

echo "<td>"
 . "<img src=\"View.php?image_id={$row['Id']}\">"
 . "</td>";

If you'll write it like this, you'd have less of a mess and it fixes the error. Clean and simple.

echo "<td><img src=\"View.php?image_id={$row['Id']}\"></td>";

When placing variables in strings it's recommended to wrap them in brackets to avoid syntax errors like these but also to keep your code looking clean.

Upvotes: 1

Krish R
Krish R

Reputation: 22731

Try this, You have string concatenation issue.

echo "<td><img src='View.php?image_id=".$row['Id']."' ></td>";

Upvotes: 0

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44874

Try as

echo '<td><img src="View.php?image_id='.$row['Id'].'"></td>';

Upvotes: 0

Related Questions