0

I am very new to SQL and PHP want to display an image on my search database along with the other results, is this possible? And could anyone offer me any assistance?

As I said I am very new to coding and am struggling slightly.

 <?php 
//load database connection
$host = "localhost";
$user = "root";
$password = "root";
$database_name = "catalog";
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, 
array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
// Search from MySQL database table
$search=$_POST['search'];
$query = $pdo->prepare("select * from catalog.final_dog_catologue_full where 
name LIKE '%$search%' OR Brand LIKE '%$search%'  LIMIT 0 , 1000");
$query->bindValue(1, "%$search%", PDO::PARAM_STR);
$query->execute();
// Display search result
     if (!$query->rowCount() == 0) {
            echo "Search found :<br/>";
            echo "<table style=\"font-family:arial;color:#333333;\">";  
            echo "<tr><td style=\"border-style:solid;border-
width:1px;border-color:#98bf21;background:#98bf21;\">Name</td><td 
style=\"border-style:solid;border-width:1px;border-
color:#98bf21;background:#98bf21;\">Brand</td><td style=\"border-
style:solid;border-width:1px;border-
color:#98bf21;background:#98bf21;\">Price</td><td style=\"border-
style:solid;border-width:1px;border-
color:#98bf21;background:#98bf21;\">Category</td><td style=\"border-
style:solid;border-width:1px;border-
color:#98bf21;background:#98bf21;\">Animal</td></tr>";              
        while ($results = $query->fetch()) {
            echo "<tr><td style=\"border-style:solid;border-
width:1px;border-color:#98bf21;\">";            
            echo $results['Name'];
            echo "</td><td style=\"border-style:solid;border-
width:1px;border-color:#98bf21;\">";
            echo $results['Brand'];
            echo "</td><td style=\"border-style:solid;border-
width:1px;border-color:#98bf21;\">";
            echo "£".$results['Retail_Price_With_Delievery'];
            echo "</td><td style=\"border-style:solid;border-
width:1px;border-color:#98bf21;\">";
            echo $results['Catogary'];
            echo "</td><td style=\"border-style:solid;border-
width:1px;border-color:#98bf21;\">";
            echo $results['Animal'];
            echo "</td></tr>";
        }
            echo "</table>";        
    } else {
        echo 'Nothing found';
    }
?>
2
  • It's absolutely possible, as you're not inserting the image to the database, but rather simply echoing data from the database. Just echo out the image along with the rest of the table; the image is just HTML. Commented Jul 10, 2017 at 23:30
  • As I said I'm very new to coding and am struggling with how/where to add the line that creates the echo that will allow me to display the picture in the table that displays. Commented Jul 12, 2017 at 21:02

1 Answer 1

1

There are two methods generally for storing images in a "database". One is to store them as a blob. Basically this is the server storing the file as a link from the database. This makes your database fat and slow very fast. The better way is to store the path to the file in the database and simply get the file you are directed to from the database entry. There can be problems with this if two files have the same name. So generally you assign the file a database identity value, preserve its file name in the database. But write it to something like the IdentityValue.xyz or something like that. You don't use normal sql to get the image file into the database. You have to use special methods for blobs (Binary Large Object Blocks).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.