Hey guys am having trouble displaying an image from the DB on a web page.
Am using this to display
echo "<img
src=image.php?"' width=300 height=270/>";
here is my image.PHP
<?php
session_start();
$con1=mysql_connect("localhost","root","roots");
mysql_select_db("blog",$con1);
if(!$con1)
{
die('could not connect'.mysql_error());
}
$q="select image from data where Number=1";
$result= mysql_query($q,$con1);
if($result)
{
$row=mysql_fetch_array($result);
header("content-type:image/jpeg");
echo $row['image'];
}
else
{
echo mysql_error();
}
?>
echo $row['image']; simply display the data's fetched from DB.
Supposed your image column holds data like this http://domain/project/images/img.jpg
echo '<img src="'.$row['image'].'" width=300 height=270 />' ;
Related
Just to preface im very new to this so this might be a dumb question but...
I am trying to design a website pulling from a database where i've stored the product info for the 'shop'. This is the code i have so far.
session_start();
include("connections.php");
$con = mysqli_connect($servername, $username, $password, $database);
$sql="SELECT product_title, product_desc, product_image, product_price, product_type FROM tbl_products";
$results=mysqli_query($con, $sql);
if(mysqli_num_rows($results) > 0) {
while($row=mysqli_fetch_array($results)) {
echo $row[0];
echo "<br>";
echo $row[1];
echo "<br>";
echo $row[2];
echo "<br>";
echo $row[3];
echo "<br>";
echo $row[4];
echo "<br>";
}
}
mysqli_close($con);
?>
It works fine at displaying the info however rather than displaying the image its just displaying the file name e.g. hoodie(1).jpg. I assume i need to save the images to the database or to the folder but i tried putting them in my code folder and they still dont show up, any ideas?
To display the image use:
echo '<img src="'.$row[2].'" alt="" />';
To upload a image, learn file uploading:
https://www.tutorialspoint.com/php/php_file_uploading.htm
i am trying to fetch an image from blob data type. this is the code i'm using
<?php
function showContent(){
global $connect;
$res= mysqli_query($connect, "SELECT * FROM services") or die(mysqli_error($connect));
while($row=mysqli_fetch_assoc($res)){
echo '<div class="col-md-3 col-sm-6 col-xs-12">';
echo '<p>'.$row['name'].'</p>';
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'"/>';
echo '<p>'.$row['content'].'<p>';
echo '</div>';
}
mysqli_close($connect);
}
showContent();
?>
this is my database table with 1 entry
i get a long string in my inspect element that is something like this
<img src="data:image/jpeg;base64,Lz.....
// this is not complete its pretty long
Please help me whats wrong with this
For base64 encode you image
$img_src = "image/sample.png";
$imgbinary = fread(fopen($img_src, "r"), filesize($img_src));
$img_str = base64_encode($imgbinary);
Print image :-
echo '<img src="data:image/jpg;base64,'.$img_str.'" />';
I need to upload and retrieve image from a database, I am able to store the image in the database but unable to display it later. please help
I wrote the following code to retrieve from the database.
$result1=mysql_query("INSERT INTO userdata(id, username, firstname, lastname, imageType, image)VALUES('', '" . $_SESSION['username'] . "', '" . $_SESSION['firstname'] . "', '$lastname','{$image_size['mime']}','{$imgData}')") or die("Invalid query: " . mysql_error());
if($result1)
{
echo "</br>";
echo "Registration successful";
echo "</br>";
echo $lastid=mysql_insert_id();//get the id of the last record
echo "uploaded image is :"; ?>
<img src="imageView.php?image_id=<?php echo $lastid; ?>" /><br/>
<?php
echo "</br>";
}#if result1into db successful
else
{
echo $result1;
echo "Problem in database operation";
imageView.php has the following code:
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("wordgraphic") or die(mysql_error());
if(isset($_GET['id'])) {
$sql = "SELECT imageType,image FROM userdata WHERE id=". $_GET['image_id'];
$result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error());
$row = mysql_fetch_array($result);
header("Content-type: " . $row["imageType"]);
echo $row["image"];
}
mysql_close($conn);
?>
What could be possibly wrong with the code?
When i try to run imageView.php with static id image is getting displayed. So i guess the error is in passing the variable is this code:
echo "uploaded image is :"; ?>
<img src="imageView.php?image_id=<?php echo $lastid; ?>" /><br/>
<?php
what could be possibly wrong?
just a smal rectification
<img src="imageView.php?image_id=<?php echo $lastid; ?>" />
instead of this
src = src path where u r saving the image file lets say in images folder that would be here $imagedata store the file name in the DB so that you can retreive it from the image folder
<img src="images/$imgData" width="your wish" height="your wish"/>
I want to show all images in my products page from a selected category when a category link is selected. My database table has 'id', 'name', 'size', 'category_id', 'image_path'. My uploading of images and saving to database and to a selected category all work correctly, I have a page where I can delete from database and that works correctly. My products.php file is below and I need assistance if some one can help.
<?php
require_once ("Includes/session.php");
require_once ("Includes/simplecms_config.php");
require_once ("Includes/connectDB.php");
include("Includes/header.php");
confirm_is_admin();
?>
<section>
<?php
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$category_id = $_GET['category_id'];
$query = mysqli_query($databaseConnection, "SELECT * FROM products WHERE category_id = '".$category_id."'");
echo '<div id="main_products">';
echo '<h1 style="margin-left: 85px;">Golf '.$category_id.'</h1><br /><br />';
$row_count = mysqli_num_rows($query);
if ($row_count == 0) {
echo '<p style="color:red">There are no images uploaded for this category</p>';
} elseif ($query) {
echo '<table style="text-align:center;">';
while($prod = mysqli_fetch_array($query))
{
echo '<tr><td>';
$file = $prod['/Golfsite/uploads/'];
$name = $prod['name'];
$id = $prod['id'];
$category_id = $prod['category_id'];
$image_path = $prod['image_path'];
echo '<img src="'.$file.'" height="150" width="175" margin="10" float="center" border="0" title="'.$name.'" />';
echo '</td></tr>';
}
echo '</table>';
}
else
{
die('There was a problem with the query: ' .$query->error);
}
?>
</div>
</section>
<br />
<?php
include ("Includes/footer.php");
?>
first check ur $_GET['category_id'] value , what is coming in this variable and if this is picking right category value then go and echo ur sql query and run this query into ur mysql database and see the result .if the result set is coming properly then the query is right otherwise there is problem in between these 2 cases.
I want to show all images from sql to my webpage.All images will come But the only
thing is it will not show every image separately.
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("buy") or die(mysql_error());
$get = "Select * from hibuy";
$get2 = mysql_query($get);
while ($row=mysql_fetch_array($get2, MYSQL_ASSOC)){
header("Content-type: image/jpeg");
echo "<div >";
echo "<img src='".$row['image']."'>"; // Here This is not Working
echo "</div>";
}
?>
Your approach is "a bit unorthodox", but this way it might work:
while ($row=mysql_fetch_array($get2, MYSQL_ASSOC)){
$imageData = base64_encode($row['image']);
// Format the image SRC: data:{mime};base64,{data};
$src = 'data:image/jpeg;base64,'.$imageData;
echo "<div >";
echo "<img src='".$src."'>";
echo "</div>";
}
Given, you have image data in your database, not the URL of the image.
In that case, just leave the header....
as seen here: http://davidwalsh.name/data-uri-php
You can try giving an id to each one of the divs or style them for margin that is greater than or equal to the image width.Assuming you image width is 50px, this code shall work.
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("buy") or die(mysql_error());
$get = "Select * from hibuy";
$get2 = mysql_query($get);
$i = 0;
while ($row=mysql_fetch_array($get2, MYSQL_ASSOC)){
header("Content-type: image/jpeg");
echo "<div style='margin-left:".$i."'>";
echo "<img src='".$row['image']."'>"; // Here This is not Working
echo "</div>";
$i+=50;
}
?>