By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,760 Members | 969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,760 IT Pros & Developers. It's quick & easy.

how to display data an image using php from a database

P: 4
hi..i have a database named abc and a table named xyz.....in the table i have fields such as id no:,name,address,profession...i need one more field that displays the image of the person along with the information...

it should display in a table format or any other good format in a page so that the image and the person's information is well displayed...

how can i do this in php...i am using php and mysql...please help asap...
Apr 9 '10 #1
Share this Question
Share on Google+
7 Replies


P: 4
hi below is the code that displays my data from the database...is there a code that i can add to this which will help me display the image of the members along with their information..

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.   $con = mysql_connect("localhost","root","");
  4. if (!$con)
  5.   {
  6.   die('Could not connect: ' . mysql_error());
  7.   }
  8.   mysql_select_db("speka", $con);
  9.  
  10.   $result = mysql_query("SELECT * FROM members");
  11.  
  12.   echo "<table border='1'>
  13. <tr>
  14. <th>IDNo:</th>
  15. <th>Name</th>
  16. <th>Course Studied </th>
  17. <th>Academic Year</th>
  18. <th>Address</th>
  19. <th>Home Address</th>
  20. <th>Employment/Profession</th>
  21. </tr>";
  22.  
  23. while($row = mysql_fetch_array($result))
  24.   {
  25.   echo "<tr>";
  26.   echo "<td>" . $row['ID No:'] . "</td>";
  27.   echo "<td>" . $row['Name'] . "</td>";
  28.   echo "<td>" . $row['Course Studied'] . "</td>";
  29.   echo "<td>" . $row['Academic Year'] . "</td>";
  30.   echo "<td>" . $row['Address'] . "</td>";
  31.   echo "<td>" . $row['Home Address'] . "</td>";
  32.   echo "<td>" . $row['Employment/Proffession'] . "</td>";
  33.   echo "</tr>";
  34.   }
  35. echo "</table>";
  36.  
  37. mysql_close($con);
  38. ?>
Apr 9 '10 #3

chathura86
100+
P: 227
i guess you store the image in the database

http://onlamp.com/pub/a/onlamp/2002/05/09/webdb2.html

check the "Delivering an Image From a Database" section

Regards
Apr 9 '10 #4

P: 4
hi..thanks chathura for the reply... i tried adding the image url...but still its not displaying the image... :(:(:( ...its showing that image icon with a cut in between for all the members....
please some1 help....

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.   $con = mysql_connect("localhost","root","");
  4. if (!$con)
  5.   {
  6.   die('Could not connect: ' . mysql_error());
  7.   }
  8.   mysql_select_db("speka", $con);
  9.  
  10.   $result = mysql_query("SELECT * FROM members");
  11.  
  12.   echo "<table border='1'>
  13. <tr>
  14. <th>IDNo:</th>
  15. <th>Image</th>
  16. <th>Name</th>
  17. <th>Course Studied </th>
  18. <th>Academic Year</th>
  19. <th>Address</th>
  20. <th>Home Address</th>
  21. <th>Employment/Profession</th>
  22. </tr>";
  23.  
  24. while($row = mysql_fetch_array($result))
  25.   {
  26.   echo "<tr>";
  27.   echo "<td>" . $row['ID No:'] . "</td>";
  28.   echo "<td>" .'<img src ="'.$imgurl.'">'; "</td>";
  29.   echo "<td>" . $row['Name'] . "</td>";
  30.   echo "<td>" . $row['Course Studied'] . "</td>";
  31.   echo "<td>" . $row['Academic Year'] . "</td>";
  32.   echo "<td>" . $row['Address'] . "</td>";
  33.   echo "<td>" . $row['Home Address'] . "</td>";
  34.   echo "<td>" . $row['Employment/Proffession'] . "</td>";
  35.   echo "</tr>";
  36.   }
  37. echo "</table>";
  38.  
  39. mysql_close($con);
  40. ?>
Apr 10 '10 #5

Atli
Expert 5K+
P: 5,058
Hey.

rinsa, where exactly is the image stored? Is the image inside the database, or does the database contain a location on the file-system where the image is stored?

If it is inside the database, you need to create a separate PHP file to fetch it. I exapain that process in this article. (See phase #4, specifically.)

If it is on the file-system, you need only fetch the location from the database and print a <img>, like you try to do on line #28 of your code. However, it is not enough to just print a $imgurl variable. You need to actually define the variable before using it, or it will be empty and no image will be found.
Apr 10 '10 #6

P: 4
hi atli...i have created a column in the table which has the name image and the datatype varchar225...i have given the location of the file-system where the image is stored in the image column....eg: C:\xampp\htdocs\HTML\images\image1.jpg....and now in the coding how should i declare the imgurl variable?

is it like $imgurl=C:\xampp\htdocs\HTML\images;

sorry i am new to php and mysql...thankyou for helping me out...
Apr 10 '10 #7

Atli
Expert 5K+
P: 5,058
Ok, so you have an absolute path to the image. That's not ideal, but we can work around that. The problem is that when you put a <img> tag in your HTML, the src attribute has to point to a URL location where the image is found. An absolute, physical location doesn't work.

When the browser gets a <img> tag, it reads the src attribute and sends another request to the server, requesting this image. Therefore, the src attribute has to point to a place where the image can be read via the HTTP server.

If you use this tag in a page at http://localhost/index.php:
Expand|Select|Wrap|Line Numbers
  1. <img src="C:\xampp\htdocs\HTML\images\image1.jpg">
The browser will try to fetch an image at:
- http://localhost/C:\xampp\htdocs\HTML\images\image1.jpg
Which will obviously not work.

What you need to do is remove the https://bytes.com/C:\xampp\htdocs part of the page create a tag that reads:
Expand|Select|Wrap|Line Numbers
  1. <img src="HTML/images/image1.jpg">
This will make the browser request:
- http://localhost/HTML/images/image1.jpg
Which, if I am understanding you setup correctly, will return the image.


To make this happen in your code, you need to do the following:
  1. Inside the while loop on line #24, where you echo the row, you need to fetch the image location from the $row into a variable.
  2. Use the str_replace function to remove the C:\xampp\htdocs part of the location.
  3. Echo the altered image location into the <img> tag on line #28.

To help you along, here is an example of how this can be done. You will need to adapt this to your own code.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Connect to a MySQL database.
  3. $dbLink = mysql_connect( 'host', 'user', 'pwd' );
  4. mysql_select_db( 'myDb', $dbLink );
  5.  
  6. // Fetch images from the database.
  7. $sql = "SELECT name, imagelocation FROM myImages";
  8. $result = mysql_query( $sql, $dbLink ) or die(mysql_error());
  9.  
  10. while($row = mysql_fetch_assoc( $result )) {
  11.     // Fetch the data for the current image.
  12.     $name = htmlentities( $row['name'] );
  13.     $imageLocation = $row['imageLocation'];
  14.  
  15.     // Remove the junk from the image location
  16.     $junk = 'C:\\xampp\\htdocs';
  17.     $imageLocation = str_replace( $junk, '', $imageLocation );
  18.  
  19.     // Replace \ slashes with / slashes.
  20.     $imageLocation = str_replace('\\', '/', $imageLocation);
  21.  
  22.     // Print the <img> tag.
  23.     echo "<img src=\"{$imageLocation}\" title=\"{$name}\">";
  24. }
  25. ?>
Apr 10 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.