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.
Related
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.
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>';
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']) . ')">';
I have stored images to SQL database with a text box and store it in database no how can I retrieve the image from database. I used the following code to display but its not working
// Grab the data from our people table
$sql = "select * from upload";
$result = mysql_query($sql) or die("Could not access DB: " . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo "<div class=\"picture\">";
echo "<p>";
// Note that we are building our src string using the filename from the database
echo "<img src=$row['data']/>";
echo $row['title'] . " " . $row['title'] . "<br />";
echo "</p>";
echo "</div>";
}
The following link may helps you.
PHP- Image upload to MySQL
set blob type for data column.
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) . '"/>";