473,322 Members | 1,846 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,322 software developers and data experts.

How do i display both text and image field from MySql using Php?

Hi all,

I am trying to display both text and image fields from MySQL in PHP page but unfortunately it doesn't work.

Here is the code i used:

Expand|Select|Wrap|Line Numbers
  1.     $id = $_GET["id"];
  2.     $sql = "SELECT * FROM user, posteditem, upload
  3.             WHERE user.userID = posteditem.userID 
  4.                 AND upload.ipID = posteditem.ipID
  5.                 AND posteditem.ipID = '$id'";
  6.  
  7.     $result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
  8.  
  9.     if (mysqli_num_rows($result) >= 1)
  10.     {
  11.         while ($recordset= mysqli_fetch_array($result)) 
  12.         {
  13.             $content = $recordset['content'];
  14.             $size = $recordset['size'];
  15.             $type = $recordset['type'];
  16.             $id = $recordset['ipID'];
  17.             /*
  18.             $userName = stripslashes($recordset['userName']);
  19.             $item= $recordset['item'];
  20.             $description= $recordset['description'];
  21.             $price= $recordset['price'];
  22.             */
  23.  
  24.             $display_block = "<form>";
  25.             $display_block = print "Seller: ".$recordset['userName']."<br>";
  26.             $display_block .= print "Contact info:<br>";
  27.             $display_block .= print "Mobile: ".$recordset['mobile']."<br>";
  28.             $display_block .= print "Tel: ".$recordset['tel']."<br>";
  29.             $display_block .= print "Email Add: ".$recordset['emailAdd']."<br><p>";
  30.             $display_block .= print "Item for sale: ".$recordset['item']."<br>";
  31.             $display_block .= print "Description: ".$recordset['description']."<br>";
  32.             $display_block .= print "Price: ".$recordset['price']."<br>";
  33.             $display_block = "</form>";
  34.  
  35.             header("Content-length: $size");
  36.             header("Content-type: $type");
  37.             //header("Content-Disposition: attachment; filename=$name");
  38.  
  39.             echo $content;
  40.  
  41.         }
  42.     }
  43. ?>
  44.  
  45. <html>
  46. <head>
  47. </head>
  48. <body>
  49. <p><?php echo "$display_block"; ?></p>
  50.  
  51. </body>
  52. </html>
  53.  
Sep 17 '07 #1
6 10767
Atli
5,058 Expert 4TB
Hi. Welcome to The Scripts!

Your code is setting the content-type of the page to something (presumably an image) and then displaying HTML. Thats all messed up!

To make this work you need at least two PHP files.

First, you need a file that gets and displays the Image data. Lets call it: Image.php. Now this file will have to receive the ID of the image to show via the GET protocol.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   # Get the ID of the image
  3.   $imageID = $_GET['imageID'];
  4.  
  5.   # Validate the ID, make sure it is in fact a number.
  6.   if(abs($imageID) != $imageID) {
  7.     die("Invalid id passed");
  8.   }
  9.  
  10.   # Connect to database
  11.   # ... You know the drill
  12.  
  13.   # Query the database
  14.   $result = mysql_query("SELECT content, mimeType FROM myTbl WHERE ImageID = $imageID") or die("Query failed". mysql_error());
  15.   if(!$result) {
  16.     echo "DB returned false result!";
  17.   } 
  18.  
  19.   # Print image
  20.   header("Content-type: ". $result['mimeType']); # or whatever your image is
  21.   header("Content-length: ". strlen($result['content']));
  22.   echo $result['content'];
  23.   exit();
  24. ?>
  25.  
Second, a file to display the data and call the Image.php file
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   # Fetch the data from the Database
  3.   # ...
  4.  
  5.   # Display all the data
  6.   echo "Seller = ". $result['seller'];
  7.   echo "Email = ". $result['email'];
  8.   echo "Description = ". $result['price'];
  9.   echo "Price = ". $result['price'];
  10.   # Etc...
  11.  
  12.   # Display the image
  13.   echo "<img src=\"Image.php?imageID={result['imageID']}\" />";
  14.  
This will obviously have to be changed a lot to work with your database, but you get the general idea.

Hope it helps.
Sep 17 '07 #2
Hi Atli,

I need to display both text and image in one page (php). Is there a way of doing that in one page?
Sep 17 '07 #3
Atli
5,058 Expert 4TB
Hi Atli,

I need to display both text and image in one page (php). Is there a way of doing that in one page?
Yes, and no. Depending on what you mean by one page.
You will have to write two separate PHP files.

But, although you are using two PHP files, all the results, including the image, will be displayed on one page.
The second page is only used to read the image. It is used in the same way you would use an image from an URL.

Read through the example of the second page I posted. Specifically where the <img> tag is echoed.
Sep 17 '07 #4
Hi Atli,

I did try your suggestion, I have 2 php files, one for the extraction of image (Image.php) and the other one for the extraction of text fields (ReplyToPost.php).

In ReplyToPost.php file, I have this code to display the image after displaying the relevant text field:
Expand|Select|Wrap|Line Numbers
  1. if (mysqli_num_rows($result) >= 1)
  2. {
  3.     while ($recordset= mysqli_fetch_array($result))
  4.     {
  5.         $display_block = "<form>";
  6.         $display_block = print "Seller: ".$recordset['userName']."<br>";
  7.         $display_block .= print "Contact info:<br>";
  8.         $display_block .= print "Mobile: ".$recordset['mobile']."<br>";
  9.         $display_block .= print "Tel: ".$recordset['tel']."<br>";
  10.         $display_block .= print "Email Add: ".$recordset['emailAdd']."<br><p>";
  11.         $display_block .= print "Item for sale: ".$recordset['item']."<br>";
  12.         $display_block .= print "Description: ".$recordset['description']."<br>";
  13.         $display_block .= print "Price: ".$recordset['price']."<br>";
  14.         $display_block .= echo "<img src=\"Image.php?id=".$recordset['id']."\>";
  15.         $display_block = "</form>";
  16.  
  17.     }
  18. }
  19.  
But I got this error:

Parse error: syntax error, unexpected T_ECHO in C:\wamp\www\tiangge\www\Posting\ReplyToPost.php

Any idea? I'm a bit newbie in PHP and it points to the image tag...
Sep 17 '07 #5
Hey,

I finally made it working. Here's what I did:

Image.php
Expand|Select|Wrap|Line Numbers
  1.             while ($recordset= mysqli_fetch_array($result)) 
  2.             {
  3.                 $content = $recordset['content'];
  4.                 $size = $recordset['size'];
  5.                 $type = $recordset['type'];
  6.                 $id = $recordset['ipID'];
  7.  
  8.                 header("Content-length: $size");
  9.                 header("Content-type: $type");
  10.                 //header("Content-Disposition: attachment; filename=$name");
  11.  
  12.                 echo $content;
  13.                 exit();
  14.  
  15.             }
  16.  
ReplyToPost.php
Expand|Select|Wrap|Line Numbers
  1.         while ($recordset= mysqli_fetch_array($result)) 
  2.         {
  3.             echo "Seller: ".$recordset['userName']."<br>";
  4.             echo "Email address: ".$recordset['emailAdd']."<br>";
  5.             echo "Mobile number: ".$recordset['mobile']."<br>";
  6.             echo "Landline: ".$recordset['tel']."<br><br>";
  7.             echo "<p></p>";
  8.             echo "Item name: ".$recordset['description']."<br>";
  9.             echo "Date posted: ".$recordset['dateposted']."<br>";
  10.             echo "Price: ".$recordset['price']."<br><br>";
  11.             echo "<p></p>";            
  12.  
  13.             echo '<img '.$recordset['id'].' src="Image.php?id='.$_GET['id'].'">';
  14. }
  15.  
Sep 17 '07 #6
Atli
5,058 Expert 4TB
I'm glad you found a solution :)
Please don't hesitate to post again if you have any more questions or problems we can help with.

P.S. Please use [code] tags when posting code. It's nearly impossible to read it without them.
Sep 17 '07 #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: 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...
0
by: Dan Anderson | last post by:
I have created a BLOB field to store images. Is there any way to embed them within HTML with something like: <image start: jpeg> </image> Thanks in advance, Dan
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...
1
by: davidgordon | last post by:
Hi, I have a form in which I need to display an image of our product. On our server is a folder which contains all the images. In our MySQL db to which our MS Access 2003 front end is linked,...
6
by: Kevin Chambers | last post by:
Hi all-- In an attempt to commit an Access MDB to a versioning system (subversion), I'm trying to figure out how to convert a jet table's metadata to text, a la SaveAsText. The end goal is to...
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...
8
tharden3
by: tharden3 | last post by:
Hey Bytes, The website I'm working on is coming along just fine, and I'd like to thank all of you PHP folks who have been helping me out. I'm almost done with the coding! I'm trying to get the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.