BigOrinj
BigOrinj

Reputation: 243

Display an Image from PHP mysql table

I have a PHP script to echo the contents of a Mysql table. I'm using it as a small CMS for a static page.

What I want to know is how can I go about displaying an Image in my PHP script. I have a form that submits the Date, Title, Message, and Image.

I'm using the Image field to insert the URL of an image. Whats the correct way of displaying the image on a page.

Below is my code:

<?php
$con = mysql_connect("localhost","name","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("databaseName", $con);

$result = mysql_query("SELECT * FROM Blog");


while($row = mysql_fetch_array($result))
  {
  echo $row['Date'];
  echo "<br />";
  echo $row['Title'];
  echo "<br />";
  echo $row['Message'];
  echo "<br />";
  echo $row['Image'];
  echo "<br />";
  echo "<br />";
  echo "<br />";
  }

mysql_close($con);
?>

This is the outcome:

17th Feb
Title
Here is my Message
http‍://vickybeeching.com/blog/wp-content/uploads/2011/09/canthearyou.jpeg


18th Feb
Title
Here is my Message
http‍://vickybeeching.com/blog/wp-content/uploads/2011/09/canthearyou.jpeg

but I want the page to display the image not the URL. I'm guessing just using the <img></img> tags, but I'm not sure where in the PHP.

Upvotes: 2

Views: 6067

Answers (3)

Sona Rijesh
Sona Rijesh

Reputation: 1035

Try this code, but give image folder path perfectly

<?php
  $imagepath = "../uploads/";
  echo "<img src='".$imagepath.$row['Image']. "' alt='' height='200' width='200' /> ";
?>

According to need , you can change height and width of the image. but clarity differs.

Thanks

Upvotes: 2

Man Programmer
Man Programmer

Reputation: 5356

 echo $row['Image'];

replace like this

 echo '<img src="'.$row['Image'].'" alt="" />';

Upvotes: 0

Hkachhia
Hkachhia

Reputation: 4539

You can write code in image tag instead of echo directly.

Try this code.

echo "<img src='".$row['Image']."'/>";

Upvotes: 2

Related Questions