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'].'" />';
}
Related
I stored an image in and SQL database as a BLOB file but when I try to echo it using
$sql = "SELECT * FROM imgtest";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<img src="data:image/jpg;base64,'.base64_encode( $row['img'] ).'"/>';
}
}
It shows only the alt image icon, I also tried echo $row['img']; but that only showed me the code of the image.
You can store your img name in a row of your table named for example "img_name" and call it like this:
$sql = "SELECT * FROM imgtest";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$my_img '<img src="'. $row['img_name'] .'"/>';
}
}
And put this in your HTML to render your image:
<?= $my_img ?>
I have this code has been working for few months without any problem. Now this page does not work and the problem from php code but I dont know where
<?php
$query = "SELECT * FROM offerimage order by name";
$result = mysqli_query($connection, $query);
if ($result->num_rows > 0) {
echo "<table>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
$image = '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'" height="100" width="100"/>';
//echo '<br></br>';
echo $image;
echo '<br></br>';
echo "</tr>";
}
} else {
echo "</table>";
}
?>
I GET THIS ERROR
"ERR_EMPTY_RESPONSE"
Why don't you output something when the query returns no rows, since that's probably the cause?
Also you should have the images inside a <td> element.
Edited to show row-by-row retrieval of images. Assumes the database table offerimage has a unique integer ID (like an AUTO_INCREMENT column) named id, change queries to match the actual table definition.
<?php
// get array of offerimage IDs ordered by name
$query = "SELECT id FROM offerimage order by name";
$result = mysqli_query($connection, $query);
$ids = [];
while ($row = $result->fetch_assoc()) {
$ids[] = (int)$row['id'];
}
if (count($ids) > 0) {
// display each image
echo '<table>';
foreach ($ids as $id) {
$query = "SELECT image FROM offerimage WHERE id = {$id}";
$result = mysqli_query($connection, $query);
$row = $result->fetch_assoc();
echo "<tr><td>";
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'" height="100" width="100"/>';
echo "</td></tr>";
mysqli_free_result($result);
}
echo "</table>";
} else {
echo "<p>No offers found.</p>";
}
Other suggestions: Don't use SELECT * if you only need one column. Use SELECT image instead.
Check for errors when making the database connection (you don't show that code) and from the query, use error_log() or send to the browser so you can see if something went wrong there.
I solve it by save images in folder and then save image location in DB.
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)
I am trying to show the blop images i have in my mysql database. But all i get is a whitebox with no picture in it.
PHP code:
<?php
require_once "include/config.php";
session_start();
$sql = "SELECT * FROM docenten";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo "<dt><strong>Foto:</strong></dt><dd>" .
'<img src="data:image/jpeg;base64,'.
base64_encode($row['foto']).
'" width="290" height="290">' . "</dd>";
}
}else{
echo "0 result";
}
?>
But when the code runs all i get is this: http://imgur.com/nhIO2LQ
Anyone know a solution?
use addslashes before insert
$img = addslashes(file_get_contents($_FILES['images']['tmp_name']));
$query = "INSERT INTO tableName (id,image) VALUES('','$image')";
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>";