Solved

PHP code to display images from MySQL db on a webpage

Posted on 2016-09-21
1
66 Views
Last Modified: 2016-09-27
Hi Experts:

Could you please help me with displaying the images stored in the database table on a webpage.  I have a database table with 8 columns as shown in the picture below.  Sometimes each row may have more than 1 image in it.  I checked the FileManager in the webhosting account and could not find the images I uploaded in the database.

Here is the code I have currently.  we need to add a code to display the images from the database for each row.


<?php
error_reporting(E_ALL);
?>
<!doctype html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>database connections</title>
    </head>
    <body>
	
<?php 
$mysqli = new mysqli('hostname', 'username', 'password', 'properties');
		
if ($mysqli->connect_error) {
	die('Connect Error (' . $mysqli->connect_errno . ') '
		. $mysqli->connect_error);
}
	  
echo 'Connected successfully'; 
     
// RETRIEVE THE type PARAMETER MAKING ALLOWANCES FOR
// THE CASE WHERE IT IS NOT PRESENT
$type = isset($_GET['type']) ? $_GET['type'] : '';

// SANITIZE THE INPUT - PREVENT SQL INJETION
// ATTACKS
$type = $mysqli->real_escape_string($type);

// CREATE THE QUERY
$query = "SELECT * FROM residences";

// IF WE HAVE A TYPE THEN FILTER ON IT OTHERWISE
// DEFAULT TO FULL TABLE
if (!empty($type)) {
  $query .= "  WHERE `type`='{$type}'";
}

// RUN THE QUERY
$result = $mysqli->query($query);
?>
<a href="residences.php?type=Residential">Residential</a>
<a href="commercial.php?type=Commercial">Commercial</a>
<a href="mypage.php?type=C">Type C</a>
      <table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
      <thead>
        <tr>
          <th>Id</th>
          <th>Name</th>
          <th>Description</th>
          <th>Type</th>
          <th>Images</th>
        </tr>
      </thead>
      <tbody>
        <?php
          while( $row = $result->fetch_assoc() ){
            echo
            "<tr>
              <td>{$row['id']}</td>
              <td>{$row['title']}</td>
	      <td>{$row['description']}</td>
	      <td>{$row['type']}</td>
            </tr>\n";
          }
        ?>
      </tbody>
    </table>
<?php $mysqli->close(); ?>
    </body>
    </html>

Open in new window

db.jpg
0
Comment
Question by:imranasif17
1 Comment
 
LVL 76

Accepted Solution

by:
arnold earned 500 total points
Comment Utility
You may need to create a php script that can be referenced as the source of info in the <Img src=Http://www.yourdomain.com/renderimage.php?reference=imageidentifier for query_string
Or you can append the info as a path renderingimage.php/sone/reference/identifier
Prior to outputting the data stream, make sure you out the data type content: image/....

How does the image stored? Might it be by reference to the file in the filesystem.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now