how to display image from mysql to php - php

I'm new to php and mysql. I created databse in mysql, and I would also like to display my image from the database to my php site, but I only get a weird code (something like this: ����JFIF,,��), everything else works as it should. This is my code:
<?php
$mysqli = new mysqli('localhost','root','','bas');
$result = $mysqli->query("SELECT * FROM bas1");
if($result->num_rows !=0)
{ while($rows = $result->fetch_assoc()
{$name=$rows['name'];
$price=$rows['price'];
$rate=$rows['rate'];
$image=$rows['image'];
echo "<tr>";
echo "<td>$name</td><td>$price</td><td>$rate</td><td>$image</td>";
echo "</tr>";}
} else {
echo "<tr>";
echo "<td>";
echo "no rusults";
echo "<td>";
echo "</tr>";}
?>
The image in databse is set to longblob. I would be very thankful if someone could help me

try this
use img tag and encode the image
echo '<img src="data:image/jpeg;base64,\'.base64_encode( $image ).\'"/>';

I recommend you to not to store an image in the database you should store image somewhere in server directory and save the path of that image in the database.
and display image like this
<img src="<?php echo $image?>">
and your image data type in your database should be text.

Related

Displaying a background-image with a longblob data from my MySQL database

I'm trying to insert a image from my database on phpmyadmin and I'm gettin a situation where it just doesnt show my image. The type I'm using on my table is a longblob because my image is bigger. Is there any way I could fix this output so I can just output my image?
NOTE : It seems everything else works EXCEPT for the image part, I just felt like showing the whole php code just in case
HTML / PHP
if ($result-> num_rows > 0) {
while ($row = $result-> fetch_assoc()) {
echo "<div class='service-title'>";
echo "<h2>". $row["serviceName"] ."</h2>";
echo "</div>";
echo "<a href=". $row["servicePageName"] .">";
echo '<div class="service-container" style="background-image: data:image/jpg;base64,'.base64_encode($row['serviceImage']).'"/>';
echo "<h1>Click Me!</h1>";
echo "</div>";
echo "</a>";
}
}
$conn-> close();
Figured it out by just adding a simple url() into the background-image

Retrieving images from MySQL Database - file path

I've got my query working, it brings me the results I expect, but one column is going to be images, and I'm unsure of the best way to go about displaying them.
Initially I used the below code to get my results, without worrying about the images.
<?php
#requires connect script - similar to include to avoid including security details
require 'connectscript.php';
#query
$sql = "SELECT * FROM products WHERE CategoryName = 'Surfboards'";
#result
$result = mysqli_query($dbc, $sql) or die ("Bad Query: $sql");
#Opening table
#while loop to bring all results
echo "<table border='2'>";
echo "<tr><td>ProductID</td><td>Name</td><td>Brand</td><td>Model</td>
<td>Board Length</td><td>Board Type</td><td>Colour</td><td>Image</td>
<td>UnitPrice</td></tr>";
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>{$row['ProductID']}</td>
<td>{$row['Name']}</td>
<td>{$row['Brand']}</td>
<td>{$row['Model']}</td>
<td>{$row['BoardLength']}</td>
<td>{$row['BoardType']}</td>
<td>{$row['Colour']}</td>
<td style='width:200px; height: 100px; overflow-hidden;'>{$row['Image']}</td>
<td>{$row['UnitPrice']}</td>
</tr>";
}
echo"</table>"
?>
I've also adjusted the code slightly with an img src in there, and it presents the broken as if it's looking for an image, but can't find one.
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>{$row['ProductID']}</td>";
echo "<td>{$row['Name']}</td>";
echo "<td>{$row['Brand']}</td>";
echo "<td>{$row['Model']}</td>";
echo "<td>{$row['BoardLength']}</td>";
echo "<td>{$row['BoardType']}</td>";
echo "<td>{$row['Colour']}</td>";
echo "<td style='width: 400px; height: 400px; overflow-hidden;'><img
src=images/{$row['Image']}</td>";
echo "<td>{$row['UnitPrice']}</td>";
echo "</tr>";
}
echo"</table>"
?>
The images are stored in an images folder, within the htdocs folder on my xampp installation. I've double checked the path. The data type us VARCHAR and i've tried with and without the extention.
Any Suggestions?
Thanks
Will

background style from mysqli database

I'm trying to add background image property do div. I know that I can display normal image from database like this:
echo '<img src="data:image/jpeg;base64,'.base64_encode($image).'" />';
but what about adding background to div? I tried, I really did but just my code doesn't work so i ask how to do that?
This is my code:
<?php
require_once('config.inc.php');
$sql = "SELECT id, title, author, image FROM articles ORDER BY count DESC LIMIT 3";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<a href="'.$row["id"].'">';
echo '<div class="module" style="background:'.base64_encode($row["image"]).'">';
echo '<h3 class="article-title">'.$row["title"].'<br />'.$row["author"].'</h3>';
echo '</div>';
echo '</a>';
}
} else echo "Wystąpił błąd z bazą danych";
$mysqli->close();
?>
"DIV" tag does'nt support an a base64 format. If you want to show an image in, you have to use the background-image property of CSS.
Example:
<div style="background-image:url(<?=$YourBase64?>);padding:50px;background-size:cover;background-position:center center; background-repeat:no-repeat;"></div>
You can see more about it in: https://www.w3schools.com/cssref/pr_background-image.asp
You are not telling your CSS to use a base64 encoded image, also remember in CSS you need the url() function:
echo '<div class="module" style="background-image:url(data:image/jpeg;base64,' . base64_encode($row['image']) . ')">';

cannot display multiple image

Image cannot be displayed in html page.
for example code :
<?php
$id = $_GET['id'];
$data =mysql_query("select * from img_homestay WHERE id='$id'");
while ($row =mysql_fetch_array($data))
{
$location = $row['location'];
echo '<img src="'.$location.'" width=30% height=10%>';
echo '<td><div align="center">Delete</div></td>';
echo "<br>";
}
?>
One obvious mistake in your code is that the closing tag '>' is missing for img. So it needs to be
echo "<img src='$location' width='30%' height='10%'>";
Apart from this be sure $location var has the correct absolute or relative path picked from DB to show the image. Then, you are using $id in the query. Be sure this is not the unique ID in the img_homestay table, as that will only return you one row. I believe you want to fetch all the images for a particular post id, so ensure that you are using that ID only for the correct field in the query.
Another suggestion that done switch between double quote and single quote string notation in PHP. This will make your code hard to read and comprehend. In one echo statement you are using
echo " ";
and in next statement you are using:
echo ' ';
Try with this
<?php
$id = $_GET['id'];
$data =mysql_query("select * from img_homestay WHERE id='".$id."'");
while ($row =mysql_fetch_array($data))
{
$location = $row['location'];
?>
<img src="<?php echo $location;?>" width="30%" height="10%">
<td><div align="center">Delete</div></td>
<br>
<?php }
?>
<?php
$id = $_GET['id'];
$data =mysql_query("select * from img_homestay WHERE id='".$id."'");
while ($row =mysql_fetch_array($data))
{
$location = $row['location'];
echo '<img src="'.$location.'" width=30% height=10%>';
echo '<td><div align="center">Delete</div></td>';
echo "<br>";
}
?>
Your processing is wrong. Your query is select * from img_homestay WHERE id='$id' which means it's going to fetch a single row (I assume, because you're making a query based on an ID which, I again assume, is a unique key) so you actually don't need to use the while loop (if you intend to use single image).
Still, if that's not the case, you may need to use <tr> for each row, so maybe, try this:
$id = $_GET['id'];
$data =mysql_query("select * from img_homestay WHERE id=$id");
while($row=mysql_fetch_array($data)){
$location = $row['location'];
echo "<tr><td><img src='$location' width=30% height=10%>";
echo "<td><div align='center'><a href='#' imgid='$row[imgid]' class='delbutton' title='Click To Delete'>Delete</a></div></td>";
echo "</tr>";
}
?>
Also, I see, you're using td here which means, you're using table but I see no table tags. My guess is, there is some issue in table structure. A better check to see if you're image is being parsed or not would be to check the source code of the rendered HTML page. I'm sure you're getting the img tag in resulting HTML but it is not being rendered due to error in HTML structure so you better check and correct your HTML with respect to the table you're using.

how to retrieve image that is stored in database?

This is my code in response of this script i get content on page that content is stored in database but please let me know the way to retrieve that content in image form?
$query='SELECT * FROM upload_file';
$result=mysqli_query($con,$query);
while($row = mysqli_fetch_array($result))
{
echo "<img src=".$row['plaint']." width='100' height='100'></img><br>";
}
Try it like this:
echo '<img src="data:image/jpeg;base64,' . base64_encode( $row['plaint'] ) . '" />';
This converts the data to the actual image.

Categories