Hello! tell me, here I have a base, there is a table with 3 fields in it - id , pic , name . The id stores the number, the pic stores the image, and the name holds the name of the image. The question is: how can I display images stored in the pic field to the site page?

 $query = "select * from tab1 where name like '%".$name."%'"; $result = mysql_query($query); $num_results = mysql_num_rows($result); echo '<p><strong>Count result : '.$num_results.'</p>'; for ($i=0; $i <$num_results; $i++) { $row = mysql_fetch_array($result); echo '<p><strong>'.($i+1).'. Id : '; echo stripslashes($row['id']); echo '<br />Image: '; echo stripslashes($row['pic']); <img src="data:image/jpg;base64,<?=base64_encode($row['pic'])?>" /> echo '<br />Image name: '; echo stripslashes($row['name']); } 

    1 answer 1

    The easiest way is:

     <img src="data:image/jpg;base64,<?=base64_encode($row['pic'])?>" /> 

    Slightly more difficult but more correct:

     <img src="/image.php?id=<?=$row['id']?>" /> 

    And the image.php file get a picture of $ _GET ['id'] from the database and output it via echo

    • And in the first version where quotes end? after base64? if so, it does not work. And before the end of the tag, too. I keep the picture in blob. - Re1aps pm
    • sorry, fixed. By the way, I hope you guessed it, that it is assumed that you have already pulled the image from the database and put it in the $ row array? - Pavel Vershinin
    • If so, it does not work: $ query = "select * from tab1 where name like '%". $ Name. "%'"; $ result = mysql_query ($ query); $ num_results = mysql_num_rows ($ result); echo '<p> <strong> Count result:'. $ num_results. '</ p>'; for ($ i = 0; $ i <$ num_results; $ i ++) {$ row = mysql_fetch_array ($ result); echo '<p> <strong>'. ($ i + 1). '. Id: '; echo stripslashes ($ row ['id']); echo '<br /> Image:'; echo stripslashes ($ row ['pic']); <img src = "data: image / jpg; base64, <? = base64_encode ($ row ['pic'])?>" /> echo '<br /> Image name:'; echo stripslashes ($ row ['name']); } - Re1aps 5:46 pm
    • Um, why did you push HTML code into PHP? Even so chtoli do: echo '<img src = "data: image / jpg; base64,'. Base64_encode ($ row ['pic']). '" />'; - Pavel Vershinin
    • Thanks, all worked well. - Re1aps