0

So, I want to make search results with image in PHP. For example, when you search for a product on a website, you get the search results with images, like this:

enter image description here

So I've created a database with one record only (type: BLOB):

enter image description here

Also, I have a PHP code for display images:

<?php
$server = mysqli_connect("localhost","root","") or die("Não consigo ligar à BD");
mysqli_select_db($server,"pesquisa") or die("Não encontro a BD");

$query = mysqli_query($server, "SELECT * FROM produtos");

while($row = mysqli_fetch_array($query)) {
        echo '<img height="300" width="300" src="data:image;base64, '.$row[2].' ">';
}

mysqli_close($server);

?>

But when I run it on a browser, the result is this:

enter image description here

Is this code wrong?

Guilherme R
  • 525
  • 1
  • 4
  • 11
  • 2
    Well, to start with, you've given us images, not code... Please add some **actual** code. Also, add source of the image that's stored in the DB, it looks like binary and not b64 – Can O' Spam May 11 '18 at 16:22
  • Side note: I don't think storing images in your database is generally a good idea. Read [this](https://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay) question's answer. – Joas May 11 '18 at 16:25
  • You should try AJAX – Riz-waan May 11 '18 at 16:26
  • ok I will actual code – Guilherme R May 11 '18 at 16:26
  • @Joas - while I agree, this does not help the OP source an answer to their issue, as it is stored in the DB, and not in the files – Can O' Spam May 11 '18 at 16:26
  • 1
    This question is a duplicate of numerous others and is too broad to be any different or more specific. Please do your research – Martin May 11 '18 at 16:26
  • 1
    @Riz-waan, how would AJAX help here? The OP can collect it in PHP on page load and save the overhead of AJAX, that feels like AJAX for AJAX sake... – Can O' Spam May 11 '18 at 16:27
  • @SamSwift웃 That is why I posted it as a comment, and not as an answer. – Joas May 11 '18 at 16:28
  • see also: [How to display Base64 images in HTML?](https://stackoverflow.com/questions/8499633/how-to-display-base64-images-in-html) – Martin May 11 '18 at 16:28
  • @Joas - I never said it *was trying to be an answer*, I merely stated it won't help *source* an answer. Besides, I agreed with you! :P – Can O' Spam May 11 '18 at 16:28
  • @SamSwift웃 No, but it is a very useful side note to the question OP is asking, and it is at least worth mentioning. As OP might rethink if he wants to store images in his database. I do not see a reason **not** to leave a small comment, that is slightly off topic, that might help OP in the feature (or right now). – Joas May 11 '18 at 16:38

0 Answers0