php image type blob wont show - php

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')";

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.

Image in the folder is deleted but image path does not updated to null in PHP

I have created a code to delete image which is in a folder and to update image_path to null in the database. Although the image deletes the image, the path does not get updated to null. I spent hours to catch my mistake. But I could not. Any help would be grateful!
This is my code
<?php
//this is were images displayed
$sql = "SELECT * FROM services WHERE user_name='wendi'";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
?>
<img src="images/template/delete.png" id="AEDbutton">delete
<?php
echo "<img border=\"0\" src=\"" . $row['image_path4'] . "\" width=\"200\" height=\"100\">";
echo "<br>";
}
}
?>
this is delete.php
<?php
include('config.php');
$sql = "SELECT * FROM services WHERE user_name='wendi'";
$result = $con->query($sql);
while ($row = $result->fetch_assoc()) {
$image = $row['image_path4'];
unlink($image);
}
$sql = "UPDATE image_path4=null, file_name4=null FROM services WHERE user_name='wendi'";
$result = $con->query($sql);
?>
It seems your UPDATE query is malformed, the syntax should be like this:
UPDATE {table} SET {column}={value} WHERE {column2}={value2}
So in your case this would be:
UPDATE services SET image_path4=null, file_name4=null WHERE user_name='wendi'
Your query should be like this:
$sql = "UPDATE services SET image_path4 = null, file_name4 = null WHERE user_name = 'wendi' ";
After uplink function assign $image to null:
$image = null;
And use update query below:
$sql = "UPDATE `services ` SET `image_path4` = ' ". $image." ', `file_name4` = ' ". $image." ' WHERE `user_name` = 'wendi' ";

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 show all the images that store in my database?

This is my sample code please help me guys
<?php
$con =mysql_connect("localhost", "" , "");
$sdb= mysql_select_db("image",$con);
$sql = "SELECT * FROM `tbl_image` WHERE id=21";
$mq = mysql_query($sql) or die ("not working query");
$row = mysql_fetch_array($mq) or die("line 44 not working");
$s = $row['img_url'];
echo '<img src="'.$s.'">';
?>
loop row.
$mq = mysql_query($sql) or die ("not working query");
while ($row = mysql_fetch_array($mq)) {
echo '<img src="' . $row['img_url'] . '" >';
}
should change your last 2 lines to this
while ($row = mysql_fetch_assoc($result)) {
$s = $row['img_url'];
echo '<img src="'.$s.'">';
}
<?php
$con = mysql_connect("localhost", "" , "");
$sdb = mysql_select_db("image",$con);
$sql = "SELECT * FROM `tbl_image` WHERE id=21";
//echo "SELECT * FROM `tbl_image` WHERE id=21"; //and run the query in database if it is working fine then you can use while loop.
$mq = mysql_query($sql) or die ("not working query"); // if this line is not working try eho the above query like i have mentioned in comments.
while ( $row = mysql_fetch_array($mq)) {
echo '<img src="' . $row['img_url'] . '" >';
}
?>
You can try following code.
while($row=mysql_fetch_object($qry))
{
$result[] = $row['img_url'];
echo '<img src="' . $row['img_url'] . '" >';
}
print_r($result)

using dynamic data in a javascript method

I'm new at php and JavaScript so I hope you can help me!
below is a php code! it take data from my db and display my photos and photo's title. what I want to do is if you click on one of this photos and than a new php expe: photo.php opens and you can see the same photo alone not with the other ones.
<?php
$link = mysql_connect("localhost", "root","");
mysql_select_db("test", $link);
$query = "select * from post order by id DESC;";
$results = mysql_query($query, $link) or die ("you comand can't be executed!".mysql_error());
if($results){
$i = 1;
while ($row = mysql_fetch_assoc($results)){
echo $row['title']. "<img src=/1/" . $row['location']. " width=580px > " . "<br /><br /><br />";
}
}
?> '
<?php
$link = mysql_connect("localhost", "root","");
mysql_select_db("test", $link);
$query = "select * from post order by id DESC;";
$results = mysql_query($query, $link) or die ("you comand can't be executed!".mysql_error());
if($results){
$i = 1;
while ($row = mysql_fetch_assoc($results)){
//send photo_id (id of photo from your table) from url NOTE: I've put link here
echo $row['title']. "<a href='index.php?photo_id=".$row['id']."'><img src=/1/" . $row['location']. " width=580px ></a> " . "<br /><br /><br />";
}
if(isset($_GET['photo_id'])) {
//here you get only one id of photo now retrieve the data from database and display the image
}
}
?>
You will get the photo by the help of the url value here photo_id.
do this way you are getting results from database and you are
displaying them with tile and image your aim is to click on
image or image title go to a new page and display image alone
the image title as link in first page
if($results){
$i = 1;
while ($row = mysql_fetch_assoc($results)){
<a href='new page url?id=<?php echo $row["id"] ?>'>
echo $row['title'];
echo "</a>"
}
}
where $row['id'] is database unique id if have no it is always preferred
to create a database with unique id and in new page get the uid
using $id=$_GET['id'] and
do as below
if($results){
$i = 1;
while ($row = mysql_fetch_assoc($results)){
if($id==$roe['id']{
echo $row['title']. "<img src=/1/" . $row['location']. " width=580px > ";
}
}
}

Categories