How to make Image clickable with URL once pulled from Blob - php

Hi I have a working code that pulls image blobs from the MYSQL database, However I want to in addtion make them click able so people can click to a product page. This is my working code so far I just haven't been able to add a Url to make the image clickable. How would I do this?
<?php
$id ='1';
$db = mysqli_connect("localhost","brianrob_usr","","brianrob_productdb"); //keep your db name
$sql = "SELECT * FROM Products WHERE id = $id";
$sth = $db->query($sql);
while($row = $sth->fetch_array()){
echo '<div><img src="data:image/jpeg;base64,'.base64_encode( $row['Image'] ).'"/></div>';
}
?>

You need to add an anchor tag around the image.
echo '<div><img src="data:image/jpeg;base64,'.base64_encode( $row['Image'] ).'"/></div>';
should do the trick presuming you have the URL as a column in the DB.

Try this:
echo '<div><img href="someurl.com" src="data:image/jpeg;base64,'.base64_encode( $row['Image'] ).'"/></div>';
Or, if it's dynamic and you have an image name in a field, do this:
echo '<div><img href="' . $row["imageName"] . '" src="data:image/jpeg;base64,'.base64_encode( $row['Image'] ).'"/></div>';

Related

how to display image from mysql to 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.

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.

How to Show Images from File System with PHP

Okay, so I have a database that stores the name of an uploaded file in a column called filename.
I am trying to use PHP to show that file (stored in the file-system, not database) by taking the filename as stored in the database and concatenating it onto the HTML tag, like so:
$stmt = $db->dbh->query("SELECT id, filename FROM images");
$stmt->setFetchMode(PDO::FETCH_BOTH);
$path = '';
while ($row = $stmt->fetch()) {
$path = 'i/' . $row['filename'];
echo "<img src=" . $path . "/>";
}
However, it's not showing anything. What am I doing wrong, or what is a better way to accomplish this?
You are missing quotes around the image file name. Try this:
echo '<img src="' . htmlspecialchars($path) . '"/>";

Categories