473,408 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to display both an Image and Data from a MySQL database in the same page.

how can i display both image and other information in the web page using php?
i'm using mysql database.
I do displaying the image only but i want to display both other information from the database and the image from the database also in one web page...
how can i do this?


pls help.....

thanks.
Nov 24 '09 #1
6 14391
Atli
5,058 Expert 4TB
Hey.

In order to display the image data from the database, it has to be printed by itself, as the image it represents. Which means it has to be fetched and printed by itself in a request that imitates an image.

We generally do what you are asking like so:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Fetch a list of images from the database.
  3. // Note, this doesn't actually fetch the image,
  4. // only the information about the image and the ID.
  5. $sql = "SELECT  id, title, description
  6.         FROM    image
  7.         LIMIT   0, 10";
  8. $result = mysql_query($sql) or die(mysql_error());
  9.  
  10. // Print a table, listing all the images
  11. echo '<table>';
  12. while($row = mysql_fetch_assoc($result))
  13. {
  14.     // Add the image data inot the row.
  15.     // Note how I use the <img> tag there.
  16.     echo <<<HTML
  17. <tr>
  18.     <td>{$row['title']}</td>
  19.     <td><img src="getImage.php?id={$row['id']}" alt="{$row['title']}" /></td>
  20.     <td>{$row['description']}</td>
  21. </tr>
  22. HTML
  23. }
  24. echo '</table>';
  25. ?>
  26.  
Where "getImage.php" is the PHP file that fetches and displays the image.
(See this article to see the theory behind that part)
Nov 24 '09 #2
Thank you for the idea atli but is there a possibility to display both the image and the data in one web page only using php? thats what i want to do here.
thanks.
Nov 25 '09 #3
Atli
5,058 Expert 4TB
Sorry for the delayed response. Been busy.

You could in-line the image data into the <img> src attribute.
For example:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title>Image Inline test</title>
  5.         <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  6.     </head>
  7.     <body>
  8.         <div>
  9.             <h3>There should be an image right below this...</h3>
  10.             <?php
  11.                 $source_image = 'test.png';
  12.                 $raw_data = file_get_contents($source_image);
  13.                 $base64_data = base64_encode($raw_data);
  14.             ?>
  15.             <img src="data:image/png;base64,<?php echo $base64_data; ?>" alt="Test Image" />
  16.         </div>
  17.     </body>
  18. </html>
This works in all the real browsers, but - shockingly - it doesn't work IE7 or lower, as well as any current version of Outlook. (Which is a shame, seeing as how useful this could be in constructing Emails.)

In any case, you are going to want to avoid that in most situations. Loading the images separate from the main page is preferable in most cases. It allows the browser to cache the images separately, and to load the layout of the site before actually loading the images, making it feel "snappier".
Nov 27 '09 #4
hi atli, where does the test.png from? i have my image save in the mysql database and save as longblob.
Dec 1 '09 #5
My mysql database table contain these following data: id, lastname, familyname, image..
In the image is where i save my image with the type of longblob...
In the browser i want to display the id, lastname, familyname and the image of the specific person.
Any php codes in doing this please...thanks a lot.
Dec 1 '09 #6
Thanks atli, i got it already...:)

You can get the ideas from here: http://www.phpbuilder.com/board/arch...-10309025.html

http://bytes.com/topic/php/answers/7...-usin-php-case

I hope it helps. :)
Dec 1 '09 #7

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

Similar topics

7
by: AF | last post by:
I am a real novice to php and MySQL, with about a week's worth of reading and self tutoring. I have an urgent need to publish a database of information and need some guidance on how to do this. ...
3
by: adam | last post by:
Hi, I have an issue that i just cant seem to figure out hope some one can help. right i am getting people to input the own images directly into a blob within a mysql database. then on the next...
3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
3
by: Christopher Mouton | last post by:
We regularly make drive images of our entire server and store it at a backup site. To be honest I have never fully understood how the imaging programs deal with open files. My question is will the...
2
by: david | last post by:
Hi, I have asp pages running from a MySQL database. I have placed a path in the required field (although not quite sure on the correct format). My asp page is just displaying the text path...
5
by: Peter Lapic | last post by:
I have to create a image web service that when it receives an imageid parameter it will return a gif image from a file that has been stored on the server. The client will be an asp.net web page...
1
by: Objectifnet | last post by:
What I really want to do is to be able to link two pages together using an ID, The table involved displays an image stored on the File Server that has the image details stored in the Database called....
7
by: eholz1 | last post by:
Hello Group, Perhaps you can help me. I have a mysql db, that holds images. Images are encoded using base64_decode/encode, etc. Image data seems fine. I have a view.php page that is supposed...
1
by: amritranjan | last post by:
How to retrive image file from MS access database and display this in another JSPpage -------------------------------------------------------------------------------- This is my Jsp code for...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.