php ,mysql image is not displaying from database - php

image is not displaying from the database it show just showing broken image
<?php
$con = mysql_connect('localhost', 'root', ''); //Update hostname
mysql_select_db("postad", $con); //Update database name
$query = "SELECT path1 FROM img_tbl";
$result = mysql_query($query) or die('Error : ' . mysql_error());
$row = mysql_fetch_array($result);
$photo = $row['path1'];
echo "<center><img src=$photo alt=Profile Photo>";
?>

$photo is path right
then try this
echo '<center><img src='.$photo.'alt="Profile Photo"></center>';

Moving from the comments, this is the fix you need:
echo '<center><img src= '.$photo.' alt="Profile Photo"></center>';

Change second last line with this
echo '<center><img src="'.$photo.'" alt="Profile Photo"></center>';
This will work if $photo path is correct...

Related

Display BLOB from mySQL with php

After I managed it to insert blobs into my db I'm trying to Display a BLOB from MySQL.
This is the getImage.php:
<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe
$link = mysql_connect("localhost", "root", "root");
mysql_select_db("user_auth_tutorial");
$sql = "SELECT image FROM testblob WHERE image_id='$id''";
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
echo $row['image'];
?>
Here the HTML:
<img src="getImage.php?id=1" width="200" height="200" />
This is the content of my db:
Unfortunately Dev-Tools throw an internal server 500 Error.
Browser-Output:
Can anybody tell me what I do wrong?
Thank you so much!
try this
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'"/>';

cannot display picture using php on the page

I am trying to dynamically display a picture based on the the entry on the database. I can see i am able to build the url from where the picture has to be fetched but it doesn't display the picture.. I am not able to figure out what is happening.. any help will be appreciated.
// Connect to the database
$dbhost = 'localhost';
$dbusername = 'vote';
$dbpasswd = 'vote';
$database_name = 'vote_active';
$connection = mysql_connect("$dbhost","$dbusername","$dbpasswd")
or die ('Couldn\'t connect to server.');
$db = mysql_select_db("$database_name", $connection)
or die('Couldn\'t select database.');
$sqlMain = ("SELECT DISTINCT number, comments, Engineer, votes FROM active_nomination;");
$lqlMain = mysql_query($sqlMain) or die(mysql_error());
while($lplMain = mysql_fetch_assoc($lqlMain)){
$enge = utf8_encode($lplMain['Engineer']);
//print $enge;
$url= "http://wwwin.kabi.com/dir/photo/prof/$enge.jpg";
print $url;
echo
'<td>;
<img src="<?php $url ?>" />
</td>';
}
?>
Change the echo line to
echo '<td><img src="'.$url.'" /></td>';
You were not properly string concatenating. Also there is no need for wrapping variables within php tags when you are already within the PHP block.

PHP Lightbox images loaded from server

Okay i`m loading images from a MySql Database server.. but when i click on it, it opens in a new page not in the lightbox popup.
// Make the connect to MySQL or die
// and display an error.
$db = mysql_connect($host, $username, $password);
if (!$db) {
die('Could not connect: ' . mysql_error());
}
// Select your database
mysql_select_db ($database);
$categorie = $_GET['categorie'];
echo "<h3>" . $categorie . "</h3><br><br>";
$query = "SELECT * FROM afbeelding WHERE categorie = '" . $categorie . "'";
$resultaat = mysql_query($query,$db) or die(mysql_error());
echo "<table>";
while ($rij = mysql_fetch_array($resultaat)){
echo "<img src=" . $rij["afbeelding"] . " width=\"150px\" height=\"150px\" class='foto' />";
}
echo "</table>";
?>
Please some help
If you're loading you images after the page is loaded, you might want to call the function that initiates the lightbox after the images arrive.
You need to set correct CSS class for your <A>, otherwise lightbox JS will not take care of it.

How to display an BLOB image stored in MySql database?

I am trying to display the last 5 images uploaded to my "store" table in MySql.
I'm a complete noob to PHP and databases and i've been reading a lot on how to do this but no luck.
I can store and display pictures one at a time but i'd like to be able to have a gallery of sorts to show the last 5 uploaded.
any advice or help would be greatly appreciated thanks!
p.s. I know it frowned upon to store pictures to a database like this but this project is just for practice.
index.php
<!DOCTYPE html>
<html>
<head>
<title>Project One</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image"> <input type="submit" value="Upload">
<form>
<p />
<?php
//connect to database
(connect to server)
(select correct DB)
//file properties
$file = $_FILES['image']['tmp_name'];
if (!isset($file))
echo "please select an image.";
else
{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = $_FILES['image']['name'];
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size==FALSE)
echo "That's not an image.";
else
{
if (!$insert = mysql_query("INSERT INTO store VALUES ('', '$image_name', '$image')"))
echo "Problem Uploading Image.";
else
{
$lastid = mysql_insert_id();
echo "Image uploaded. <p />Your image:<p /><img src=get.php?id=$lastid>";
}
}
}
?>
<p />
<p />
Go to Gallery
</body>
</html>
get.php
<?php
//connect to database
(connect to server)
(select correct DB)
$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM store WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/jpeg");
echo $image;
?>
This is what I used when I wanted to do something like that... a long time ago! =P
$sql = "SELECT image FROM table WHERE cond ORDER BY xxxx DESC LIMIT 5";
$result = mysqli_query($db,$sql);
while($arraySomething = mysqli_fetch_array($result))
{
echo "<img src='php/imgView.php?imgId=".$arraySomething."' />";
}
I try the first approach with header('content-type: image/jpeg'); but end up with image not shown. After a few google through website I found the solution which I can display image from database to my page
try this:
mysql_connect("localhost","root","")or die("Cannot connect to database"); //keep your db name
mysql_select_db("example_db") or die("Cannot select database");
$sql = "SELECT * FROM `article` where `id` = 56"; // manipulate id ok
$sth = mysql_query($sql);
$result=mysql_fetch_array($sth);
// this is code to display
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image_file'] ).'"/>'
mysql_connect("localhost","root","")or die("Cannot connect to database");
//keep your db name
mysql_select_db("example_db") or die("Cannot select database");
$sql = "SELECT * FROM `article` where `id` = 56";
// manipulate id ok
$sth = mysql_query($sql);
$result=mysql_fetch_array($sth);
// this is code to display
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image_file'] ).'"/> width="xxxx" height="xxxx"';
Add the height and width also
You can also use this function
//Retrieve image from database and display it on html webpage
function displayImageFromDatabase(){
//use global keyword to declare conn inside a function
global $conn;
$sqlselectimageFromDb = "SELECT * FROM `imageuploadphpmysqlblob` ";
$dataFromDb = mysqli_query($conn, $sqlselectimageFromDb);
while ($row = mysqli_fetch_assoc($dataFromDb)) {
echo '<img height="250px" width="250px" src=data:image;base64,'.$row['image'].'/>';
}
Insert it into mysql database like this :
$image = $_FILES['imagefile']['tmp_name'];
$name = $_FILES['imagefile']['name'];
$image = base64_encode(file_get_contents(addslashes($image)));
references : https://mauricemutetingundi.blogspot.com/2019/04/how-to-upload-blob-image-to-mysql.html

moving away from using BLOB to using referencing path

Instead of storing images using BLOB in mySQL, I have decided to attempt to use a referencing path instead. So I stored images into my "localhost" folder:
localhost/dvd_artwork/image.jpg
Therefore in my database, under column "dvdimage_path" I have TEXT "dvd_artwork/image.jpg" where my id = 1
However, I can't seem to get this to work at the moment
my catalog.php:
<img src="getImage.php?id=1" alt="" width="175" height="200" />
my getimage.php:
<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe
$link = mysql_connect("localhost", "root", "");
mysql_select_db("dvddb");
$sql = "SELECT dvdimage_path FROM dvd WHERE id=$id";
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);
echo $row['dvdimage_path'];
?>
is there anything wrong?
remove quotes from mysql_query
// Change that
$result = mysql_query("$sql");
// To that
$result = mysql_query($sql);
This doesn't make any sense – getImage returns a string, not an image...
You probably want to replace the getImage href, and just generate the page like this:
<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe
$link = mysql_connect("localhost", "root", "");
mysql_select_db("dvddb");
$sql = "SELECT dvdimage_path FROM dvd WHERE id=$id";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
mysql_close($link);
?>
<img src="<?= $row['dvdimage_path'] ?>" alt="" width="175" height="200" />
...or you want to use file_get_contents on the path, then echo that out with the appropriate headers:
<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe
$link = mysql_connect("localhost", "root", "");
mysql_select_db("dvddb");
$sql = "SELECT dvdimage_path FROM dvd WHERE id=$id";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
mysql_close($link);
header('Content-Type: image/jpeg'); // Or png, or whatever...
echo file_get_contents($_SERVER['DOCUMENT_ROOT'].$row['dvdimage_path']); // might need a slash between the two variables here...
?>
I'd recommend the first way if possible, as it then has the benefits of allowing normal header usage via your server config.

Categories