Can't Display Longblob images with php in cpanel [duplicate] - php

This question already has an answer here:
Displaying BLOB image from Mysql database into dynamic div in html
(1 answer)
Closed 3 years ago.
$con= mysqli_connect("host", "user", "password", "database");
session_start();
if(isset($_SESSION['username'])){
//echo 'Hi! '.$_SESSION['username'];
}
if(isset($_GET["name"])){
$n= "SELECT * FROM `users` WHERE `user_name`='".$_GET['name']."' "[0];
$r= mysqli_query($con, $n);
if($r){
while($row= mysqli_fetch_assoc($r)){
echo $row['Full_name'];
$image= $row['profile'];
echo $image;
}
}
}
Here is my Code. When I run it it just shows scrambled lines(!!)
What can I do to display the images which are saved as Longblob in mysql database.
This is a web-based project, coded in cpanel hosting. I tried some solutions but they didn't work. I think the problem happens in the links
I tried this solution:
<img src="data:image/jpeg;base64,<?php echo base64_encode( $image ); ?>" />

Don't use blob to store images. Use varchar to save URL of the image and upload that images to specific folder. MySQL is not designed to store images.

Related

How to display an image in PHP? [duplicate]

This question already has answers here:
PHP display image BLOB from MySQL [duplicate]
(2 answers)
How to retrieve images from MySQL database and display in an html tag
(4 answers)
Closed 4 years ago.
So, I want to make search results with image in PHP. For example, when you search for a product on a website, you get the search results with images, like this:
So I've created a database with one record only (type: BLOB):
Also, I have a PHP code for display images:
<?php
$server = mysqli_connect("localhost","root","") or die("Não consigo ligar à BD");
mysqli_select_db($server,"pesquisa") or die("Não encontro a BD");
$query = mysqli_query($server, "SELECT * FROM produtos");
while($row = mysqli_fetch_array($query)) {
echo '<img height="300" width="300" src="data:image;base64, '.$row[2].' ">';
}
mysqli_close($server);
?>
But when I run it on a browser, the result is this:
Is this code wrong?

Image retrieved in PHP is not displaying properly

I am having trouble with displaying images with php. I have tried this question, but it's still not displaying properly.
I have a PHP file that is handling the HTML format, the code for the image part is as follows:
while($row = mysql_fetch_array($result)) {
$partNo = $row['partNo'];
echo "<tr>";
echo '<td> <img src="getThumb.php?id =\''. $partNo .'\' width="50" height="50" /> </td>';
My getThumb.php is as follows:
<?php
$id = $_GET['id'];
$con = mysql_connect("localhost", "root", "root") or die("Unable to connect to server");
mysql_select_db("bmworld.mu");
$sql = "SELECT thumbnail FROM part WHERE partNo=$id" or die ("Could not fetch thumbnail");
$result = mysql_query("$sql");
$row = mysql_fetch_object($result);
mysql_close($con);
header("Content-type: image/jpeg");
echo $row['thumbnail'];
?>
The results is as follows:
http://postimg.org/image/xqcq16ucf/
My image is of type 'blob' saved in MySQL, and not a path to the image folder. I want to retrieve that blob from MySQL, and display it directly from the retrieved blob instead of a path to the image, just like the other columns of data from my database table. How can I do this?
There are following problems may be in your code:-
your image path is not correct (what you are saving in your database).
Either you are saving your image directly with blob data-type and at the time retrieval you face some problem.
For the first problem try to hardcode path of your image and check, if working then your path saving to databsae is not correct and you have to correct.
For second problem please check this link:PHP display image BLOB from MySQL. It will definitely help you. Thanks.
are u sure that getThumb.php has good file location?
<img src="getThumb.php?id =\''. $partNo .'\' width="50" height="50" />

Add an Image in a table mysql

The main idea is being able to controll, upload and delete a slideshow's images from a database.
So, first I connect to the database like this, and with a while function I echo all the images I have stored in my db.
This is the code to do that:
<?php
$servername = "myservername";
$username = "myusername";
$password = "mypassword";
$dbname = "mydbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "SELECT id, img FROM mytable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<img src=".$row['img'].">";
}
} else {
echo "0 results";
}
?>
In the table "mytable" I have 2 columns: an ID auto-inc and a column called img.
How can I store an image in the img column so that my function will echo all the images I have in my table?
All I found searching til now was a lot of methods that upload the image with an html form, but I'd prefer to upload my img directly on my database, without building an html+php form to upload all my images to my table.
Is that possible?
Storing entire images in a database is generally a bad idea. It is much simpler to simply store the path of the image inside your database, as has already been mentioned. You may have a bit of extra work deleting the images, as you would need a php system call to delete the image file.
If you really must store images inside a database, you can save them as base64 encoded text. I've seen this be done at a fairly successful company, but no particularly good reason was given for this. Your sql execution time will be substantially larger, because images are large files, and your table size will balloon.

Displaying PNG images from MySQL

I'm trying to display an image from a MySQL database and my code works fine if the image is a JPG, but if I modify the content-type to display a PNG, it doesn't work...
How do I make it work for PNG??
<?php
// configuration
require("../includes/config.php");
header('Content-type: image/JPG');
$username = "xxxxx";
$password = "*****";
$host = "localhost";
$database = "database";
#mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());
#mysql_select_db($database) or die("Can not select the database: ".mysql_error());
$query = mysql_query("SELECT * FROM images");
while($row = mysql_fetch_array($query))
{
echo $row['image'];
}
?>
Also, is it possible to display the image along with its name and description?
Thanks!
If you don't mind not supporting older browsers, you can use data urls to display your images and a description http://en.wikipedia.org/wiki/Data_URI_scheme.
<figure>
<img src="data:image/png;base64,<?php echo base64_encode($row['image']); ?>" alt="Your alternative text" />
<figcaption>Some more description</figcaption>
</figure>
Saving images in a database is not very useful in almost all cases. You should be carefull with upper and lower letters in the mimetype, see http://de.selfhtml.org/diverses/mimetypen.htm (it's german, but you will be able to read the list). And as a last advice - look at the mysqli_* functions http://www.php.net/manual/en/book.mysqli.php.
// Edit: Just an idea, but if you have multiple images in the database your image might be broken, because you just put them all into one image. This will not work! With your code, you can only display one image at once.

Displaying images from a mysql database

i am relatively new to php. I have a problem with displaying images in my browser ( google chrome) after retrieving blob data from mysql database.
Basically the following code works when the slashes are added in front of the echo at the bottom. However i have followed other online tutorials and the tutor has been able to display their images without the use of slashes whilst i am unable to get the image up. I just wondered what the standard rule is? Another thing to add - when i do fail to get the images up in the browser i get instead a ting thumbnail. I would really appreciate if anybody could tell me how to reliably display images. The site i wish to create is just about images. So its kind of fundamental. Thanks a lot in advance for your time.
<?php
$conn = mysql_connect ("localhost","root","arctic123");
$db = mysql_select_db ("user_images", $conn);
if(!$db) {
echo mysql_error();
}
$q = "SELECT * FROM images";
$r = mysql_query ("$q",$conn);
if($r) {
while($row = mysql_fetch_array($r)) {
//echo $row ['username'];
//echo "<br>";
header ("Content-type: image/jpeg");
echo $row ['logo'];
//echo "<br>";
}
}else{
echo mysql_error();
}
?>
You can't have header after any output in your code: http://php.net/manual/en/function.header.php
Best practice is to upload images to a directory and just store the image's path/file name in the database. Also makes it easier to manipulate the image, e.g. create different sizes and thumbnails with PHP. And it takes about four times less the disk space...
Hope you are not storing your images on MySQL, please if you do, desist from that detrimental act because it is going to make your database unnecessarily heavy...
I would recommend not storing your images in a database. While it is technically possible, it has serious performance concerns. Best practice would be to designate a folder for the images, then access them directly. If you'd like the filtering and sorting ablities of MySQL, then replace the BLOB column that currently stores the image data with a VARCHAR containing the file name.
<?php
$conn = mysql_connect ("localhost","root","arctic123");
$db = mysql_select_db ("user_images", $conn);
$imgdir = "/path/to/image/directory/";
if(!$db) {
echo mysql_error();
}
$q = "SELECT * FROM images";
$r = mysql_query ("$q",$conn);
if($r) {
while($row = mysql_fetch_array($r)) {
echo $row['username'];
echo "<br>";
echo "<img src='" . $imgdir . $row['logo'] . "' />";
echo "<br>";
}
}else{
echo mysql_error();
}
?>

Categories