I need my PHP page to show my BLOB image from mysql database
In your current case, you have two upfront options.
The first, and the one I don't recommend if you have numerous images like this, is to use inline base64 encoding. This is done with:
<img src="data:image/jpeg;base64,<?php echo base64_encode($image); ?>" />
A copy/paste version, using your existing code:
echo '<dt><strong>Technician Image:</strong></dt><dd>'
. '<img src="data:image/jpeg;base64,' . base64_encode($row2['image']) . '" width="290" height="290">'
. '</dd>';
The second method is to create an "image" PHP file that takes the ID of the image in the database as a query-string parameter and outputs the image. So, your HTML would look something like:
<img src="image.php?id=<?php echo $image_id; ?>" />
And your PHP page would look something similar to:
<?php
$id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? intval($_GET['id']) : 0;
$image = getImageFromDatabase($id); // your code to fetch the image
header('Content-Type: image/jpeg');
echo $image;
?>
Display all images stored as BLOB data from MySQL database
First you need to remove the if(isset($_GET['id'])
statement because you want to display all images.
Next, query all images by changing your query to query without an id
$query = mysql_query("select * from `blob`");
Next, store the image data to an array.
$images = array();
while ($row = mysql_fetch_assoc($query)) {
$images[] = $row['image'];
}
Finally, display all images. (Got the code to display multiple blobs from here)
/* header should be removed
header("content-type: image/jpeg"); */
foreach ($images as $image) {
echo '<img src="data:image/jpeg;base64,'. base64_encode($image) .'" />';
}
PHP display image BLOB from MySQL
Try it like this.
For inserting into DB
$db = new mysqli("localhost", "root", "", "DbName");
$image = file_get_contents($_FILES['images']['tmp_name']);
$query = "INSERT INTO products (image) VALUES(?)";
$stmt = $db->prepare($query);
$stmt->bind_param('s', $image);
$stmt->execute();
For accessing image from Blob
$db = new mysqli("localhost", "root", "", "DbName");
$sql = "SELECT * FROM products WHERE id = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param('s', $id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_array();
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'"/>';
Displaying BLOB image from Mysql database into dynamic div in html
1) Base64 option
Work with a single line, image/png
for a png image and image/jpeg
for a jpg one :
echo '<img src="data:image/png;base64,'.base64_encode($blob).'"/>';
example :
<div style="background-color:black; text-align:center; padding: 5px;"> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAwBAMAAACh2TSJAAAALVBMVEUAAADtNTX////3n5/+9fX719f7zMz5tLTzfHzuQED//f31jY3ybGzxXV3wVFRaxp+rAAAAAXRSTlMAQObYZgAAALVJREFUOMut0rENAjEQRNHdC4kY0QBaAQUQX0QAFSAKIKQEKiAA6VqgIkriApuV1x7pQPz0aWwHljLMpZ0CRDBGoXmeghGYKFJsUo90giAImCgV5OJF+oOgKE48MlGgs2VLBIunWesw0a1ZHqF82c7GmmIfUSpgotOly29DFPFJFDEhkgIT/V5mZuvj6XofKrHU6vyI4u37IYi36aN4h5tL7PJyif1dvCgEpapzISbCTEj5R78BZq5A5Ldh2XYAAAAASUVORK5CYII"></div>
Related Topics
Best Solution to Protect PHP Code Without Encryption
Where Are $_Session Variables Stored
Can Png Image Transparency Be Preserved When Using PHP'S Gdlib Imagecopyresampled
Find a Matching or Closest Value in an Array
How to Find the Mime Type of a File With PHP
Why Is Facebook PHP Sdk Getuser Always Returning 0
Insert/Update Helper Function Using Pdo
How to Find the PHP.Ini File Used by the Command Line
How to Extend Access Token Validity Since Offline_Access Deprecation
What Are the Differences in Die() and Exit() in PHP
Explode PHP String by New Line
Are Arrays in PHP Copied as Value or as Reference to New Variables, and When Passed to Functions