PHP retrieve pictures from server - php

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>";
}
?>

Related

Error ERR_EMPTY_RESPONSE when display image

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.

Create dynamic table in php with mysql data

I would to like to create a dynamic table that the rows and columns of the table depends on the data in mysql data. Please see below my codes.
<?php
require "connect/db.php";
$query = mysqli_query($mysqli, "SELECT COUNT(level) level, shelf_no, bin_id FROM location_bin WHERE rack_id =1 GROUP BY shelf_no");
while($res=mysqli_fetch_array($query)){
$col = $res['level']; //col
$row = $res['shelf_no']; //rows
echo "<table border='1'>";
$i = 0;
while ($i < $col){
if ($i==$col){
echo "<tr>";
}
echo "<td>".$res['bin_id']."</td>";
$i++;
}
echo "</tr>";
echo "</table>";
}
?>
What I want is to display A-1-01 up to A-1-06 in the first row then A-2-01 to A-2-03 on the second row. Note that the data is dynamic.
Can you try this code . It works. change db.php path as your system path.
require "db.php";
$query = mysqli_query($conn, 'SELECT level, shelf_no, bin_id FROM location_bin ORDER BY shelf_no asc, level asc ');
echo "<table border='1'>";
echo "<tr>";
$i=0;
while($res = mysqli_fetch_assoc($query)) {
$row=$res['shelf_no'];
if ($i < $row){
$i=$row;
echo "</tr><tr>";
}
echo "<td>".$res['bin_id']."</td>";
}
echo "</table>";
?>

How to display image in a row from database?

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>";
?>

Displaying data horizontally using php and mysql

Hi I'm attempting to display data retrieved from a mysql table horizontally in an html table using php. The code below works well except for the fact that it leaves out the first record (starts at the second record) in my database. I'm sure it has something to do with the counter but I can't seem to figure out how to get it to stop doing this. If anyone can point out my error I'd really appreciate it!
$items = 5;
$query = "SELECT * FROM members ";
$result = mysql_query($query)
or die(mysql_error());
$row = mysql_fetch_array($result);
if (mysql_num_rows($result) > 0) {
echo '<table border="1">';
$i = 0;
while($row = mysql_fetch_array($result)){
$first_name = $row['first_name'];
if ($i==0) {
echo "<tr>\n";
}
echo "\t<td align=\center\">$first_name</td>\n";
$i++;
if ($i == $items) {
echo "</tr>\n";
$i = 0;
}
}//end while loop
if ($i > 0) {
for (;$i < $items; $i++) {
echo "<td> </td>\n";
}
echo '</tr>';
}//end ($i>0) if
echo '</table>';
}else {
echo 'no records found';
}
try and remove the 1st
$row = mysql_fetch_array($result);
you are calling it twice, that's why it skips 1 row in your while loop
try this simpler.
$items = 5;
$query = "SELECT * FROM members ";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
echo '<table border="1">';
while($row = mysql_fetch_array($result)){
$first_name = $row['first_name'];
echo "<tr>";
for ($i=0 ; $i <= $items ;$i++) {
echo "<td align='center'>".$first_name."</td>";
}
}//end while loop
echo "</tr>";
echo '</table>';
}else{ echo 'no records found'; }
I have run into this issue before. Try the do while loop instead. Example
do {
// code
} while($row = mysql_fetch_array($result)); //end while loop
$row = mysql_fetch_array($result);
1) remove this line of code from ur scripts
2) only use while loop code instead.

mysql_fetch_array not fetching complete data?

I have a code which fetches data from a mysql table and converts it into pdf document, the code is working fine except it is skipping row 1.
Here is the code from which i have removed the pdf generation process since the problem is in the loop which is fetching data.
Please help.
<?php
session_start();
if(isset($_SESSION['user']))
{
$cr = $_POST['cour'];
$s = $_POST['sem'];
require('fpdf.php');
include('../includes/connection.php');
$sql = "SELECT * FROM `student` WHERE AppliedCourse ='$cr'";
$rs = mysql_query($sql) or die($sql. "<br/>".mysql_error());
if(!mysql_fetch_array($rs))
{
$_SESSION['db_error'] = "<h2><font color = 'RED'>No such course found! Pease select again.</font></h2>";
header('Location: prinrepo.php');
}
else {
for($i = 0;$i <= $row = mysql_fetch_array($rs);$i++)
{
$formno[$i] = $row ['FormNo'];
$rno[$i] = $row ['rollno'];
$snm[$i] = $row ['StudentNm'];
$fnm[$i] = $row ['FathersNm'];
$mnm[$i] = $row ['MothersNm'];
$addr[$i] = $row['Address'];
$pic[$i] = $row['imagenm'];
$comm[$i] = $row['SocialCat'];
echo $formno[$i]."<br />";
echo $rno[$i]."<br />";
echo $snm[$i]."<br />";
echo $fnm[$i]."<br />";
echo $mnm[$i]."<br />";
echo $addr[$i]."<br />";
echo $pic[$i]."<br />";
echo $comm[$i]."<br />";
echo "<br />";
}
}
mysql_close($con);
}
?>
You are fetching the first row outside of your for() loop then you miss it.
After mysql_query() your should use mysql_num_rows() to check if there are any rows in your result and then fetch them in the for loop.
More info here : http://php.net/manual/fr/function.mysql-num-rows.php
Your code would look like this :
$sql = "SELECT * FROM `student` WHERE AppliedCourse ='$cr'";
$rs = mysql_query($sql) or die($sql. "<br/>".mysql_error());
if(0 == mysql_num_rows($rs)) {
$_SESSION['db_error'] = "<h2><font color = 'RED'>No such course found! Pease select again.</font></h2>";
header('Location: prinrepo.php');
} else {
for($i = 0;$i <= $row = mysql_fetch_array($rs);$i++)
{
// Your code
}
}

Categories