2

I am trying to get an image that is saved as a URL in a MySQL database to display on a webpage as an image rather than a URL. With the code below it displays as a URL:

<?php 
// Connects to your Database 
mysql_connect("server","username","password") or die(mysql_error()); 
mysql_select_db("database") or die(mysql_error()); 

$id=$_GET["id"]; // Collecting data from query string
if(!is_numeric($id)){ // Checking data it is a number or not
echo "Data Error";
exit;
}
$data=mysql_query("SELECT * FROM `table_name` WHERE `User_ID`=$id");
$row=mysql_fetch_object($data);
echo mysql_error();


echo"<tr><td colspan='2'>$row->Market_Name</td></tr>";
echo"<tr><td colspan='2'>$row->Days</td></tr>";
echo"<tr><td colspan='2'>$row->Time</td></tr>";
echo"<tr><td colspan='2'>$row->Description</td></tr>";
echo"<tr><td>$row->Image</td><td>$row->Google_Maps</td></tr>";  
echo"<tr><td>$row->Website</td><td>$row->Facebook</td></tr>";


?> 

If I change this piece of code:

echo"<tr><td>$row->Image</td> 

to:

echo"<tr><td><img src='Image/".$row['Image']."' width='150' height='80'>

I get the following error:

PHP Fatal error: Cannot use object of type stdClass as array

Is there a way using object rather than array to display an image that is saved as a URL in a MySQL database?

2
  • what this print ?? echo"<tr><td>$row->Image</td> Commented Sep 7, 2013 at 11:48
  • For $row['Image'] to work you'd have had to use mysql_fetch_array or mysql_fetch_assoc instead of mysql_fetch_object. Commented Sep 7, 2013 at 11:56

2 Answers 2

1

You are just exporting the URL from your database rather than telling it to display as an image.

I'd have thought this would do the trick

echo "<tr><td><img src='".$row->Image."'></td>";
Sign up to request clarification or add additional context in comments.

Comments

1

You are getting that error because your variable is of object type and you are trying to access it as an array.

So use this instead to access that value,

echo"<tr><td><img src='Image/".$row->Image."' width='150' height='80'>

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.