Quinlanlent Quinlanlent - 3 months ago 78
PHP Question

Displaying BLOB image from database yii 2

Hello I am using a table for the blob images that contains the images and a client column to reference it the question is how can I display an image blob based on the clientid so that I can show it in the webpage using yii 2?

Here is a screenshot of the table.

enter image description here

Because in this particular article I got the impression it is only uploading the image as BLOB.
Yii2 Display image stored in BLOB database field

But how do I make it so that I would only returning/displaying the blob from the database?

EDIT 1

I converted the answer into yii code.

Controller

public function DisplayBlob($clientid){
$model = new Slimages();

return $model->DisplayBlob($clientid);

}


Here is the model.php

public static function DisplayBlob($clientid){
return static::find()->where(['clientid' => $clientid])->asArray()->all();
// $db = $db = mysqli_connect("localhost","root","","myenrollment"); //keep your database name
// $sql = "SELECT * FROM slimages WHERE clientid = $clientid"; // pass your clientid here
// $statement = $db->query($sql);
// $result = mysqli_fetch_array($statement);
// return $result;
//echo '<img src="data:image/jpeg;base64,'.base64_encode($result['Picture'] ).'"/>';
}


View file

<td colspan="2"><?php
$result = SiteController::DisplayBlob($_user);
foreach($result as $image){
// echo 'hello';
echo '<img src="data:image/jpeg;base64,'.base64_encode($image['Picture'] ).'" height="100" width="100"/>';
}


?></td>

Answer
$db = mysqli_connect("localhost","root","","database"); //keep your database name
$sql = "SELECT * FROM table_name WHERE clientid = $id"; // pass your clientid here
$statement = $db->query($sql);
$result = mysqli_fetch_array($statement);
echo '<img src="data:image/jpeg;base64,'.base64_encode($result['Picture'] ).'"/>';

Note : Please change the db details , table and column names as per your requirement.

i hope this will help you. Feel free to comment.