Solved

PHP code to display images from MySQL db on a webpage

Posted on 2016-09-21
1
85 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 77

Accepted Solution

by:
arnold earned 500 total points
ID: 41809892
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

803 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