0

I am creating a real estate database and I know that using a PHP MySQL database would be a good way to go. I have a table in MySQL which has multiple fields which are text and numbers, I have no problem displaying them on my webpage.

However the image file (saved as a BLOB) won't display properly so I just end up getting coding in my table (the image coding).

So what I would like to do is simply to get some coding which can take the BLOB image file from the database table (which has all the property details and images stored within the one table - I ideally don't want more than one table).

And then I want my web page to put the image straight into the website (so I want it to display as an image, then I will need to somehow get the image to the size I want using HTML.

Current coding can be found here:

<?php

class Table extends RecursiveIteratorIterator {
    function __construct($it) {
        parent::__construct($it, self::LEAVES_ONLY);
    }

    function current() {
        return "<td style='width:300px;border:1px solid black;'>" . parent::current(). "</td>";
    }

    function beginChildren() {
       echo "<table style='border: solid 5px black;'>";
        echo "<tr><th>ID</th><th>Area</th><th>Bedrooms</th><th>People</th><th>Pool</th><th>Type</th><th>Currency</th><th>Price</th><th>Img</th></tr>";
    }

    function endChildren() {
        echo "</tr>" . "<br>" . "\n";
    }
} 

This is where server names etc. go:

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT ID, area, bedrooms, people, pool, type, currency, price, image FROM properties");
    $stmt->execute();

    // set the resulting array to associative
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
    foreach(new Table(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
        echo $v;
    }
}
catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>

I would really appreciate any suggestions. I don't want to have to do separate tables etc for this because the whole point of having this database is to manage the data as if I were an estate agent, I need this to be quick and easy to add properties and images alike and I need the website to display them in the pages easily.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    Take a look at this answer on StackOverflow: [Proper header php mysql blob display image][1] [1]: http://stackoverflow.com/questions/20714163/proper-header-php-mysql-blob-display-image – Jack Albright Aug 03 '15 at 15:17
  • I don't fully understand the question, but using multiple tables in a database-powered application is usually best, as it reduces data redundancy to a minimum. You can create forms in PHP that make it look like the data is stored in just one table, but in fact the different elements - buyer, seller, property, image - come from different tables. – halfer Aug 07 '15 at 09:39

0 Answers0