Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP code to display images from MySQL db on a webpage

Posted on 2016-09-21
1
Medium Priority
?
109 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 80

Accepted Solution

by:
arnold earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
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 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…
Suggested Courses

916 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