Unable to display image from MySQL database using php - php

I have a Products_Images table which stores the images in BLOB format. I want to show all the images and product details in 'View All Products' page. To accomplish this task I have created the following program, but its not giving me the desired output. Kindly check it.
Thanks.
<?php
include 'connect.php';
$query= "select distinct product_id, image from product_images";
$query_run= mysql_query($query);
while ($query_fetch= mysql_fetch_assoc($query_run))
{
echo '</br>';
echo $query_fetch['image'];
}
header("Content-type: image/jpeg");
?>
Displaying The Image
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<br />
<img src="all_images.php" alt="Image" width="50" height="50" />
<body>
</body>
</html>

When you are using header() function you cannot print anything prior to a header call as this directs the web server to prepare a content header
<?php
include 'connect.php';
$query= "select distinct product_id, image from product_images";
$query_run= mysql_query($query);
header("Content-type: image/jpeg");
while ($query_fetch= mysql_fetch_assoc($query_run))
{
// echo '</br>';
echo $query_fetch['image'];
}
?>
Note: Please avoid using mysql* they are depreciated in php newer
version keep your hands on using PDO or at least mysqli

why you store it in database, store your image in a folder and enter the name in the database, while calling the image in the src just write the path of the folder and php code where you mention the name.
<img src="yourfolder/<?php echo your database table field name where images names are stored?>" />

Related

PDF of large size is not displaying , while the small one(PDFs having one page) is getting displayed. I am using PostgreSql database here

<?php
$cn= pg_connect('host=localhost port=5432 dbname=sampledb user=postgres
password=xxxx');
$id = $_GET['id'];
$query=pg_query($cn,"select * from books where id=$id");
while($row=pg_fetch_array($query))
{
$id = $row['id'];
$book_pdf=$row['book_pdf'];
$pdf=pg_unescape_bytea($row['book_pdf']);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<object data="data:application/pdf;base64,<?php echo $pdf; ?>"
type="application/pdf" style="height:1000px;width:100%">
</object>
</body>
</html>
I encode my PDFs in base64 format and then save it in PosgreSql bytea column and then I give pg_unescape_bytea() to extract base64 data. Can someone tell how to handle large amount of base64 data.

Picture references from MySql

i am trying to buid a Php site that show data from my MySQl database.
and i think im almost there, everything works except the Pictures.
i cant get my php site to show the pictures with the picture reference from Sql database.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
$con=mysqli_connect("localhost","root","","db1");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<head>
<link rel="stylesheet" href="styles.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>untitled</title>
</head>
<body>
<div class="content">
<?php
$sql = "SELECT id, Producent, Model, kategori FROM tb1";
$result = mysqli_query($con,"SELECT * FROM `tb1");
echo "<table>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>";?> <img scr="<?php echo $row["Billedurl"]; ?>"/> <?php echo " </td>";
echo "<td>" .$row["Producent"] .$row["Model"]; echo "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<!-- end .content --></div>
<!-- end .container --></div>
<div class="footer"><br>
<!-- end .footer --></div>
</body>
</html>
You need to select all the columns you are going to use, so if you need Billedurl, you should change:
$sql = "SELECT id, Producent, Model, kategori FROM tb1";
to:
$sql = "SELECT id, Producent, Model, kategori, Billedurl FROM tb1";
Now the value of that column will be available in $row["Billedurl"].
Edit: It seems that now you have the correct value in your html, but the path to the image is not correct as it is a relative path.
You should prefix your variable with the correct folder so that the image is found by the browser. That can be as simple as just using an absolute path but that depends on where the pic/ directory is located.
So if your variable contains pic/l_jabra_evolve80.jpg" and the pic/ folder is on the root of the web-server, you can do something like:
# before the loop
$imagePrefix = '/';
# in the loop
... <img scr="<?php echo $imagePrefix . $row["Billedurl"]; ?>"/> ...
Now the browser will try to fetch the image from /pic/l_jabra_evolve80.jpg.
You need to do little bit of debugging:
first change little bit in your query
$sql = "select * FROM tb1";
$result = mysqli_query($con, $sql);
Then try to check print_r($result) and check if you are getting everything here or not.

How to select any specific item from Database and show it in a different page

I am creating a program in which I have a list of products. I want a user to click on any specific product and see its details. I have written the following code. I am grabbing the current 'product_id' into $_SESSION variable and displaying its details in the next page. Its working,but not giving me the desired output, its grabbing the same id for each product. I think its because i am storing its value in $_SESSION. Kindly check it and tell me the correct way of doing it.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
<?php
session_start();
include 'connect.php';
$query= mysql_query ("select product_id,name, price, description from products" );
while ($query_array= mysql_fetch_assoc($query))
{
echo '</br><pre>';
$_SESSION['id']= $query_array['product_id'];
$product_id= $_SESSION['id'];
echo "<a href='single_product.php?product_id=$product_id' >";
print_r ($query_array['name']);
echo "</a>";
print_r ($query_array['price']);
echo "<img src= 'all_images.php' alt= 'Image'";
echo '</pre>';
}
?>
you need to define the $row in $_session and not $_session to $row
$_SESSION['id']= $query_array['product_id']; //<---- must reverse it.
$product_id= $_SESSION['id'];
try this
$product_id = $query_array['product_id'] ;
$product_id = $_SESSION['id'];
Or you can pass the ID via $_GET
in your code
echo "<a href='single_product.php?product_id=$product_id' >";
you can get the product_id like that in the other file
echo $_GET['product_id'] ;

arranging images in xampp database

i have this code in php. i want my images displayed to be horizontally displayed. those images are saved in xampp. can anybody help me? this is my code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
mysql_connect("localhost","root","");
mysql_select_db("dbLetters");
$res=mysql_query("select * from tbLetters");
echo "<table>";
while ($row = mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>";?> <img src = "<?php echo $row["images"]; ?>" height="200" width="200"> <? php echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
your response will be a big help. thanks. :))
Unless you use a data url, you actually need to set up a separate script to pull that will provide the image to the browser.
This will make your image url something like image.php?id=<image_id>. The image.php script will fetch the image and provide the image to the browser.
Its not usually a good idea to store image data in mysql.

getting image with php and displaying in html

i know it is so simple problem but eventually it isn't working and i am a newbie. in the index.html, a swf sends an image and displayImage.php(below code) should display it on another page. why isn't it working??
<?php
if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {
$no=0;
while (file_exists("images/$no.jpg"))
$no++;
header('Content-Type: image/jpeg');
$image = $GLOBALS["HTTP_RAW_POST_DATA"];
file_put_contents("images/".$no.".jpg", $image);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml2/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-9" />
<title>Your Image</title>
<link href= "style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="logo"></div>
<div id="body"></div>
/////////display image//////////
<img src="images/<?$no.".jpg?>">
</body>
</html>
You don't echo the filename (and you have a quoting error, but this could be a typo):
<img src="images/<?php echo $no ?>.jpg" />
Assuming that storing the file actually works.
<img src="images/<?=$no.".jpg"?>">
This might be the issue. The line below the "display image" comment, should probably read:
<img src="images/<?= $no ?>.jpg">
Could be that you're using XHTML strict. IMG tags can't be unclosed like in normal HTML. You have to end it with a /> instead. (it's supposed to fail catastrophically when you make an error but it only does that when you send the right MIME type header to treat it as XML, so treated as text you may get unpredictable results.....). If it's printing the name of the file (like images/1.jpg) then you know that it's parsing the inline PHP correctly....
i deleted the line "header('Content-Type: image/jpeg');" and it works!

Categories