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" />
Related
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.
I am trying to display an image using php. The image is stored in MYSQL table. I am able to retrieve the information from mysql in my php code, but i am having trouble displaying the image.
$db_link = mysql_connect($host_name, $user_name, $password) or die("Could not connect to $host_name");
mysql_select_db("gameportal") or die("Could not select database $db_name");
$query = "select * from gamesinfo;";
$result = mysql_query($query, $db_link) or die("Could not select");
$Row = mysql_fetch_row($result);
echo "$Row[0]<br>";
echo "$Row[1]<br>";
echo "<img src="$Row[7]" class="body" alt="" /> <br>";//image
echo "$Row[5]<br>";
Row 7 contains the location of the image (in this case a weblink). When i try to display the webpage, the page is blank, nothing shows, but when i remove that line with the pic, the webpage shows with the remaining info. What am i doing wrong?
This is the culprit:
echo "<img src="$Row[7]" class="body" alt="" /> <br>";
You use unquoted double quotes inside double quotes ;-). Try
echo "<img src='$Row[7]' class='body' alt='' /> <br>";
EDIT
The point is not the double quotes inside double quotes, but unquoted double quotes inside double quotes - this should work as well:
echo "<img src=\"$Row[7]\" class=\"body\" alt=\"\" /> <br>";
I missed this the first time but:
<?php
$db_link = mysql_connect($host_name, $user_name, $password) or die("Could not connect to $host_name");
mysql_select_db("gameportal")bor die("Could not select database $db_name");
You've got bor instead of or. Make sure to turn PHP errors on.
Try this
Images is a directory where the images are stored.
$dir="images/";
**
echo "<img src='$dir/$row[image]' width=100px height=100px>";
**
It works fine.
Not an exact answer.... but a small piece of advice. I have written an answer because I don't have the reputation for commenting. You can turn on error reporting with this line at the top of php script.
error_reporting(-1);
Such kinds of errors would be displayed on screen and you would be able to debug yourself. When your work is done you can simply do this...
error_reporting(0);
Refer this link: PHP error reporting
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.
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();
}
?>
I am having trouble displaying images from my db, when i show it without the header i get the image data but when i give it the header('Content-type: image/jpeg'); nothing is displayed.
in my db i have the image column as a longblob.
$host='******';
$user='******';
$pass='******';
$db='******';
$tbl_name='******';
//connect to db
mysql_connect($host, $user, $pass);
mysql_select_db($db);
$id=addslashes($_REQUEST['id']);
$image=mysql_query("SELECT * FROM $tbl_name WHERE id='$id'");
$image=mysql_fetch_assoc($image);
$image=$image['image'];
header('Content-type: image/jpeg');
echo "<img src='$image' />";
?>`
Remove the img tag. You want to output the raw data only:
echo $image;
Note that addslashes() is not adequate protection against SQL injection. You would have to use mysql_real_escape_string() (or intval()).