Problem when trying to retrieve image by name

  • joy1986joy
  • Novice
  • Novice
  • User avatar
  • Posts: 27

Post 3+ Months Ago

I want to retrieve image from database without storing the image data. Just storing the image path I want to retrieve or show the image if anybody can help
Code: [ Select ]
<?php
//hear I will retrieve image path from database. For testing purpose I have given it manually.
$img_path="images/a.jpg";
//hear I will print the image.
echo "<img src='$img_path' />";

?>
  1. <?php
  2. //hear I will retrieve image path from database. For testing purpose I have given it manually.
  3. $img_path="images/a.jpg";
  4. //hear I will print the image.
  5. echo "<img src='$img_path' />";
  6. ?>


I also need to write some html in the same page.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Are you basically asking how to connect to a database and retrieve information?
  • joy1986joy
  • Novice
  • Novice
  • User avatar
  • Posts: 27

Post 3+ Months Ago

No. I am asking for displaying the image by calling it from the image folder. I don't want to store the image in the database. I want to store only the location of the image. Then I want to pass the image location by a php variable after retrieving it from database. So I have retrieve it. But I am not able to display it.
by this line:
Code: [ Select ]
echo "<img src='$img_path' />";
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

So the code in your last post does not display the image?

What value are you storing in the database? If the image isn't being displayed your probably not calling the image from the root of the domain, which in a lot of cases is what it needed.
  • joy1986joy
  • Novice
  • Novice
  • User avatar
  • Posts: 27

Post 3+ Months Ago

I am storing the image path. Like "images/abc.jpg"
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

Do this: http://www.w3schools.com/php/php_mysql_select.asp

PHP Code: [ Select ]
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("my_db", $con);
 
$result = mysql_query("SELECT src FROM images");
 
while($row = mysql_fetch_array($result))
  {
  echo '<img src="'.$row['src'].'" />';
  echo '<br />';
  }
 
mysql_close($con);
?>
  1. <?php
  2. $con = mysql_connect("localhost","peter","abc123");
  3. if (!$con)
  4.   {
  5.   die('Could not connect: ' . mysql_error());
  6.   }
  7.  
  8. mysql_select_db("my_db", $con);
  9.  
  10. $result = mysql_query("SELECT src FROM images");
  11.  
  12. while($row = mysql_fetch_array($result))
  13.   {
  14.   echo '<img src="'.$row['src'].'" />';
  15.   echo '<br />';
  16.   }
  17.  
  18. mysql_close($con);
  19. ?>
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Your going to need to store either the complete path to the image or at least add on the complete path.

If the images folder is at the root of your domain all you will have to do is add a slash infront /images/abc.jgg
  • joy1986joy
  • Novice
  • Novice
  • User avatar
  • Posts: 27

Post 3+ Months Ago

Got the solution. thanks everybody for help. Now I can retrieve images from data base by their name.

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: Bogey and 109 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.