Image display from database - php

I am trying to display an image from database with blob datatype,
I tried all code from all sites and videos but I can't get a result.
$db = new mysqli ("localhost","root","","catchyourbook");
$sql = mysqli_query($db,"SELECT * FROM `diplo computer engg`");
$result = mysqli_fetch_array($sql);
echo '<img src="data:image/png;base64,'.base64_encode( $result['Front page'] ).'"/>';
echo "<tr>";
echo "<td><img src='$row[Front page].png' height="150px" width="300px"></td>";
echo "</tr>\n";

Related

No img showing for mysql query and no stocknr when hovering

Having a problem uploading a picture from a database, wondering if i'm doing something wrong..
I've checked that the image is there by using <img scr='/upload/imgpath.jpg'>
Any suggestions?
$sql1 = "SELECT pic, stocknr, status FROM stock WHERE status = '1'";
$result = mysqli_query($con,$sql1);
while($row1 = mysqli_fetch_assoc($result));
{
$stocknr2 = $row1['stocknr'];
$picname = $row1['pic'];
$upfile = $picname;
echo "<center>";
echo "<a href='details.php?stocknr=$stocknr2'>";
echo "<img src='/upload/$upfile' border=1 width=350 height=280></a>";
echo "</center>";
}
mysqli_free_result($result);
You just need to change while row;
while($row1 = mysqli_fetch_assoc($result)

How to fetch all datas and images from mysql tableusing PDO?

I'm trying to get a list of news from MySQL but I have some issues with PDO and I can not get. Actually I have titles, messages, photos and categories. Please see the code below and if you can help me. Images are in folder named 'userdatas'
$sth = $conn->prepare("SELECT * FROM news");
$sth->execute();
while($row = $sth->fetch(PDO::FETCH_ASSOC)){
echo $row['id'];
echo $row['title'];
echo $row['category'];
echo $row['message'];
echo '<img src="' . $row['photo'] . '" height="60" width="40"> ';
echo "<br>";
}
The problem with the above code is that you are only specifying the image name. You need something that will specify the full path to the image like this:
$sth = $conn->prepare("SELECT * FROM news");
$sth->execute();
while($row = $sth->fetch(PDO::FETCH_ASSOC)){
echo $row['id'];
echo $row['title'];
echo $row['category'];
echo $row['message'];
echo '<img src="PATH/TO/IMAGE/' . $row['photo'] . '" height="60" width="40"> ';
echo "<br>";
All you need to do is replace PATH/TO/IMAGE with the actual folder path, or change the database to include the full path

PHP : fetching image from mysql database (blob type)

i am trying to fetch an image from blob data type. this is the code i'm using
<?php
function showContent(){
global $connect;
$res= mysqli_query($connect, "SELECT * FROM services") or die(mysqli_error($connect));
while($row=mysqli_fetch_assoc($res)){
echo '<div class="col-md-3 col-sm-6 col-xs-12">';
echo '<p>'.$row['name'].'</p>';
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'"/>';
echo '<p>'.$row['content'].'<p>';
echo '</div>';
}
mysqli_close($connect);
}
showContent();
?>
this is my database table with 1 entry
i get a long string in my inspect element that is something like this
<img src="data:image/jpeg;base64,Lz.....
// this is not complete its pretty long
Please help me whats wrong with this
For base64 encode you image
$img_src = "image/sample.png";
$imgbinary = fread(fopen($img_src, "r"), filesize($img_src));
$img_str = base64_encode($imgbinary);
Print image :-
echo '<img src="data:image/jpg;base64,'.$img_str.'" />';

How to add bootstrap grid with database result using php

I am newbie in php. i am using the following code to get my desired data from mysql database. but what i want to do, whenever database show a result i want to put it into a bootstrap grid( like col-sm-4) each time. Right now my grid is coded in HTML, how can i generate it with the query result each time ? thanks in advance.
<div class="col-sm-4">
<h3>image </h3><br>
<?php
$sql = "SELECT * FROM sctable";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$link = "http://localhost/sc/uploads/" .$row[1];
echo "<img width='100%' height='200' src=$link />"."<br />";
echo "";
}
?>
</div>
Do you mean something like this where you just want to put each rows data into its own div
<?php
$sql = "SELECT * FROM sctable";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo '<div class="col-sm-4">';
echo '<h3>image </h3><br>';
$link = "http://localhost/sc/uploads/" .$row[1];
echo '<img style="width:100%;height:200px" src="' . $link . '"/>';
echo '</div>';
}
?>
Just for future reference, its a bad idea to use the full url for your own site in code like this. If you move it to a live site this code wont work anymore. Better to use relative paths and let the system do some of the work for you. So it should work whatever the domain is where you move the code.
<?php
$sql = "SELECT * FROM sctable";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo '<div class="col-sm-4">';
echo '<h3>image </h3><br>';
> Changed next line
$link = "sc/uploads/" .$row[1];
echo '<img style="width:100%;height:200px" src="' . $link . '"/>';
echo '</div>';
}
?>
You can just wrap the image:
echo "<div class='col-sm-4'> <img width='100%' height='200' src='$link' /></div>";

PHP MYSQL BASIC QUESTION, IF QUERY EMPTY, HIDE IMAGE

I ask a php mysql basic question. I make a query and echo for images. in my code, if '.$row['image'].' is empty, it will show a small red cross image in the browser. how to hide the image if the query is empty?
$result = mysql_query("SELECT * FROM table WHERE catalog='image'");
while ($row = mysql_fetch_array($result))
{
echo '<img src="'.$row['image'].'" />';
}
$result = mysql_query("SELECT * FROM table WHERE catalog='image'");
while ($row = mysql_fetch_array($result))
{
if(!empty($row['image'])){
echo '<img src="'.$row['image'].'" />';
}
}
Why don't you just check if $row['image'] is empty?
if(!empty($row['image'])){
echo '<img src="'.$row['image'].'" />';
}
$result = mysql_query("SELECT * FROM table WHERE catalog='image'");
while ($row = mysql_fetch_array($result))
{
if(trim($row['image']) != '')
echo '<img src="'.$row['image'].'" />';
}

Categories