472,364 Members | 2,086 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 software developers and data experts.

how to display data an image using php from a database

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
7 11233
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
  3.   $con = mysql_connect("localhost","root","");
  4. if (!$con)
  5.   {
  6.   die('Could not connect: ' . mysql_error());
  7.   }
  8.   mysql_select_db("speka", $con);
  10.   $result = mysql_query("SELECT * FROM members");
  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>";
  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>";
  37. mysql_close($con);
  38. ?>
Apr 9 '10 #3
227 100+
i guess you store the image in the database


check the "Delivering an Image From a Database" section

Apr 9 '10 #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
  3.   $con = mysql_connect("localhost","root","");
  4. if (!$con)
  5.   {
  6.   die('Could not connect: ' . mysql_error());
  7.   }
  8.   mysql_select_db("speka", $con);
  10.   $result = mysql_query("SELECT * FROM members");
  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>";
  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>";
  39. mysql_close($con);
  40. ?>
Apr 10 '10 #5
5,058 Expert 4TB

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
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
5,058 Expert 4TB
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 );
  6. // Fetch images from the database.
  7. $sql = "SELECT name, imagelocation FROM myImages";
  8. $result = mysql_query( $sql, $dbLink ) or die(mysql_error());
  10. while($row = mysql_fetch_assoc( $result )) {
  11.     // Fetch the data for the current image.
  12.     $name = htmlentities( $row['name'] );
  13.     $imageLocation = $row['imageLocation'];
  15.     // Remove the junk from the image location
  16.     $junk = 'C:\\xampp\\htdocs';
  17.     $imageLocation = str_replace( $junk, '', $imageLocation );
  19.     // Replace \ slashes with / slashes.
  20.     $imageLocation = str_replace('\\', '/', $imageLocation);
  22.     // Print the <img> tag.
  23.     echo "<img src=\"{$imageLocation}\" title=\"{$name}\">";
  24. }
  25. ?>
Apr 10 '10 #8

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

Similar topics

by: bborden | last post by:
Novice Access programmer here. I would like to display an image using the Toolbox Image object by calling the images file name using: =fPictureFiles(!!,1) in the Picture control. Below...
by: grzybek | last post by:
Hi, How can I display data from SQL in my page as link in C#. I want to display name of files as link to my files on the server and then for example open or save it on the local PC. Thanks in...
by: lalithabhamidi | last post by:
I am trying to display an image using MagickimageDisplay using C API. But it asks for X server.So i am unable to display my image. Any Suggestions??
by: zeeshanks | last post by:
Can anyone please advise how to fill a dataset with data returned from an oracle based sql query? Thanks, Zee
by: jeet123 | last post by:
hi, i am new to c#. How can i make a form which displays data in a tabular format where the data is retrieved from the database (ms-access) and displayed in that from in table. No matter if the...
by: swetha123 | last post by:
hello, Can any one please tell me I am Using php,Mysql,CSS to build my site In my database i have small images and large images i am show the small images to the right of the main container...
by: priyamtheone | last post by:
Is it possible to add, edit, view and delete records using a file only (instead of a database) and .Net 2005 (VB.Net/C#)? The concept is that a file that'll work more-or-less like a database. One...
by: selvasoft | last post by:
Hi Please help me i want display all images from my database. but here my code that will display only one image from database. But i want all images, any one help me. <html> <head>...
by: gaurav13477 | last post by:
i fetch single image . how i fetch multiple image. please send the code. img1.php <?php $con=mysql_connect('localhost','root',''); $d=mysql_select_db("test"); $q1="select * from cat ";...
by: vindu | last post by:
hi guys i have got a problem in designing my web pages....... That the for example background-image:url('sun.jpeg'); is not working Can any one help me finding out solution for this....and give...
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.