Sanzeeb Aryal
Sanzeeb Aryal

Reputation: 3266

image not displayed from database, shows only unusual fonts

I have been trying to display the image from database but it shows only unusual fonts in place of image. I have tried this code:

$db = new mysqli("localhost", "root", "", "learndb");
$stmt=$db->prepare("SELECT * FROM studentrecords WHERE id=?");
$stmt->bind_param("i",$id);
$stmt->execute();
$result=$stmt->get_result();
$myrow = $result->fetch_assoc();
echo '<img src="'.$myrow["image-data"].'" >';

Upvotes: 1

Views: 78

Answers (1)

user5723483
user5723483

Reputation:

Your solution is in this link here, where you should add:

echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';

Into your code:

$db = mysqli_connect("localhost","root","","DbName"); //keep your db name
$sql = "SELECT * FROM studentrecords WHERE id=$id";
$sth = $db->query($sql);
$result=mysqli_fetch_array($sth);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';

So in your case:

$db = mysql_connect("localhost", "root", "");
mysql_select_db("learndb",$db);
$result = mysql_query("SELECT * FROM studentrecords WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $myrow['image-data'] ).'"/>'

And try not to use MySQL anymore, you can use MySQLi or PDO.

Upvotes: 2

Related Questions