I want to retrieve the image from the database and want to display it in a row like if there is 20 images in a database then it should get display in 5 rows containing 4 images in each row...
My code is working properly but i am facing problem in image path as i hv stored the image-name in database and image inside admin/uploads folder.... like uploads folder is inside the admin folder... adminfolder/uploadsfolder/file-name
here in my code i am getting problem in tracing the image path.....
I am Facing problem in this line..
// my all images is in admin/uploads folder
echo "<td><img src=\"".$path[$i]."\" height='100'/></td>";
Here is my full code
<?php
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('dogs_db') or die(mysql_error());
$sql = "SELECT file_name FROM dogsinfo";
$res = mysql_query($sql) or die(mysql_error());
echo "<table><tr>";
while($fetch = mysql_fetch_assoc($res)) {
$path[] = $fetch['file_name'];
}
for($i=0;$i<count($path);$i++){
if($i%4 == 0){
echo "</tr>";
}
// I am Facing Problem here ...
// my all images is in admin/uploads folder
echo "<td><img src=\"".$path[$i]."\" height='100'/></td>";
}
echo "</tr></table>";
?>
Please help me to sort out this....
How are within the admin/uploads folder, you must put in the src of the img tag.
<?php
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('dogs_db') or die(mysql_error());
$sql = "SELECT file_name FROM dogsinfo";
$res = mysql_query($sql) or die(mysql_error());
echo "<table><tr>";
while($fetch = mysql_fetch_assoc($res)) {
$path[] = $fetch['file_name'];
}
for($i=0;$i<count($path);$i++){
if($i%4 == 0){
echo "</tr>";
}
// I am Facing Problem here ...
// my all images is in admin/uploads folder
echo "<td><img src=\"admin/uploads/".$path[$i]."\" height='100'/></td>";
}
echo "</tr></table>";
?>
Related
Just to preface im very new to this so this might be a dumb question but...
I am trying to design a website pulling from a database where i've stored the product info for the 'shop'. This is the code i have so far.
session_start();
include("connections.php");
$con = mysqli_connect($servername, $username, $password, $database);
$sql="SELECT product_title, product_desc, product_image, product_price, product_type FROM tbl_products";
$results=mysqli_query($con, $sql);
if(mysqli_num_rows($results) > 0) {
while($row=mysqli_fetch_array($results)) {
echo $row[0];
echo "<br>";
echo $row[1];
echo "<br>";
echo $row[2];
echo "<br>";
echo $row[3];
echo "<br>";
echo $row[4];
echo "<br>";
}
}
mysqli_close($con);
?>
It works fine at displaying the info however rather than displaying the image its just displaying the file name e.g. hoodie(1).jpg. I assume i need to save the images to the database or to the folder but i tried putting them in my code folder and they still dont show up, any ideas?
To display the image use:
echo '<img src="'.$row[2].'" alt="" />';
To upload a image, learn file uploading:
https://www.tutorialspoint.com/php/php_file_uploading.htm
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)
So here is the set up:
I am running on a local host environment (all permissions are fine). The project I am working on has to retrieve photos from the server and output them in a table. I do this by saving image paths on a mysql DB (I know mysql is deprecated...) and outputting them via PHP like so,
<div class='images'>
<?php
$error = "";
$query = "SELECT imagepath FROM stories "; //replace image path with user
if (mysql_num_rows(queryMysql($query)) == 0) $error='no images found';
else {
$query2 = "SELECT imagepath FROM stories "; //replace image path with user
$result = queryMysql($query2);
$array = array();
echo "<table id='imagebox'>
<tr>";
while ($rows = mysql_fetch_array($result, MYSQL_NUM)) {
$array[] = $rows;
}
for ($j = 0; $j < count($array); $j++ ) {
echo "<td><img id='photo' src='$array[$j]' height='100' width='100'></td>";
if (($j %3) == 0 ){
echo "</tr><tr>";
}
}
echo "</table>";
}
?>
In short, the output works. I get the table I want, but the image doesn't display (I see the box where it should be with the broken image symbol). Its not a problem with the stored link, because I have tested it. Help is appreciated. (PS ignore the comments in the code, they have nothing to do with my predicament).
Your problem is either:
1. The url of the image is not in the database or
2. The image data is in the database, not the image path
My guess is that on the server you are storing relative links instead of absolute links, ie, /image1.jpg instead of www.yourserver.com/image1.jpg.
can you try this :
in database imagepath juste the name of file
<div class='images'>
<?php
$error = "";
$query = "SELECT imagepath FROM stories "; //replace image path with user
if (mysql_num_rows(queryMysql($query)) == 0) $error='no images found';
else {
$query2 = "SELECT imagepath FROM stories "; //replace image path with user
$result = queryMysql($query2);
$array = array();
echo "<table id='imagebox'>
<tr>";
while ($rows = mysql_fetch_array($result, MYSQL_NUM)) {
$array[] = $rows;
}
for ($j = 0; $j < count($array); $j++ ) {
echo "<td><img id='photo' src='./images/$array[$j]' height='100' width='100'></td>";
if (($j %3) == 0 ){
echo "</tr><tr>";
}
}
echo "</table>";
}
?>
I want to show all images from sql to my webpage.All images will come But the only
thing is it will not show every image separately.
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("buy") or die(mysql_error());
$get = "Select * from hibuy";
$get2 = mysql_query($get);
while ($row=mysql_fetch_array($get2, MYSQL_ASSOC)){
header("Content-type: image/jpeg");
echo "<div >";
echo "<img src='".$row['image']."'>"; // Here This is not Working
echo "</div>";
}
?>
Your approach is "a bit unorthodox", but this way it might work:
while ($row=mysql_fetch_array($get2, MYSQL_ASSOC)){
$imageData = base64_encode($row['image']);
// Format the image SRC: data:{mime};base64,{data};
$src = 'data:image/jpeg;base64,'.$imageData;
echo "<div >";
echo "<img src='".$src."'>";
echo "</div>";
}
Given, you have image data in your database, not the URL of the image.
In that case, just leave the header....
as seen here: http://davidwalsh.name/data-uri-php
You can try giving an id to each one of the divs or style them for margin that is greater than or equal to the image width.Assuming you image width is 50px, this code shall work.
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("buy") or die(mysql_error());
$get = "Select * from hibuy";
$get2 = mysql_query($get);
$i = 0;
while ($row=mysql_fetch_array($get2, MYSQL_ASSOC)){
header("Content-type: image/jpeg");
echo "<div style='margin-left:".$i."'>";
echo "<img src='".$row['image']."'>"; // Here This is not Working
echo "</div>";
$i+=50;
}
?>