I have a PHP script to echo the contents of a Mysql table.
I'm using it as a small CMS for a static page.
What I want to know is how can I go about displaying an Image in my PHP script.
I have a form that submits the Date, Title, Message, and Image.
I'm using the Image field to insert the URL of an image.
Whats the correct way of displaying the image on a page.
Below is my code:
<?php
$con = mysql_connect("localhost","name","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databaseName", $con);
$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
echo $row['Date'];
echo "<br />";
echo $row['Title'];
echo "<br />";
echo $row['Message'];
echo "<br />";
echo $row['Image'];
echo "<br />";
echo "<br />";
echo "<br />";
}
mysql_close($con);
?>
This is the outcome:
17th Feb
Title
Here is my Message
http://vickybeeching.com/blog/wp-content/uploads/2011/09/canthearyou.jpeg
18th Feb
Title
Here is my Message
http://vickybeeching.com/blog/wp-content/uploads/2011/09/canthearyou.jpeg
but I want the page to display the image not the URL.
I'm guessing just using the <img></img> tags, but I'm not sure where in the PHP.
You can write code in image tag instead of echo directly.
Try this code.
echo "<img src='".$row['Image']."'/>";
Try this code, but give image folder path perfectly
<?php
$imagepath = "../uploads/";
echo "<img src='".$imagepath.$row['Image']. "' alt='' height='200' width='200' /> ";
?>
According to need , you can change height and width of the image. but clarity differs.
Thanks
echo $row['Image'];
replace like this
echo '<img src="'.$row['Image'].'" alt="" />';
Related
i built a CMS and my image shows succesfully in the admin page(back-end) once i upload an image with the form but once on the main index page(front-end) it shows that "broke image link"
here is my function to post the uploaded image on my admin page:
function gettestimony() {
$query = mysql_query("SELECT * FROM testimony") or die(mysql_error());
while($post = mysql_fetch_assoc($query)){
echo "<img src =\"" . $post['photo']."\">";
echo "<p>" . $post['imagename'] . "</p>";
echo "<br>";
echo "<p>" . $post['test'] . "</p>";
echo "<br>";
echo "<p>" . $post['author'] . "</p>";
echo "<br>";
echo "Delete";
echo "Edit";
echo "<br>";
}
}
and the directory is htdoc/blah/admin/include/functions.php
and the image is saved in htdoc/blah/admin/image/
for my main index, this is the function to post it:
function gettestimony() {
$query = mysql_query("SELECT * FROM testimony") or die(mysql_error());
while($post = mysql_fetch_assoc($query)){
echo "<div class='column second'>";
echo "<p>" . $post['test'] . "</p>";
echo "<img src =\"" . $post['photo']."\">";
echo "</div>";
}
}
this is the code i use in both functions:
echo "<img src =\"" . $post['photo']."\">";
so like i said it works fine in admin page but my main page it doesn't....someone please help
Your image's path relative to the path of your function is irrelevant. As far as that function is concerned the path to the image is just a string.
You need to make sure the path to the image is correct relative to the HTML file that's viewing it. The easy way to make it work is to just use the full URL to the image:
www.something.com/admin/images/myimage.php
i have store an image into my database.
but i am unable to display the image what i have done till now is under.... any help will be appreciated. thanks in advance!
<?
$query="SELECT * from testimonial";
$ret = mysqli_query($mysql,$query);
if (isset($ret) && $ret->num_rows>0)
{
while($row=mysqli_fetch_array($ret))
{
$body=$row['body'];
$name=$row['name'];
$image=$row['img'];
?>
<li>
<div class="frame-icon"><? echo "<img src=test_img.php?id=".$row['id']." width=150 height=150/>";?></div>
<p class="quote"><?php echo $body; ?><span><?php echo $name; ?></span></p>
</li>
<?php }
echo "</table>";
}
?>
and my test_img code is
<?
<?php if (isset($_GET['id'])){
$id=mysql_real_escape_string($_GET['id']);
$query=mysql_query("SELECT *FROM testimonial WHERE id='$id' ");
while($row = mysql_fetch_assoc($query))
{
$image=$row["img"];
}
header("content-type: image/png");?>
hi Abhik i have try this one but i get something like this
and in my case i have png image so just change the jpeg to png the code you have given
but i got this <img src="data:image/png;base64,iVBORw0KGgpcMFwwXDANSUhEUlwwXDBcMGRcMFwwXDBkCAZcMFwwXDBw4pVUXDBcMFwwCXBIWXNcMFwwCxNcMFwwCxMBXDCanBhcMFwwCk9pQ0NQUGhvdG9zaG9wIElDQyBwcm9maWxlXDBcMHjanVNnVFPpFj333vRCS4iAlEtvUhUIIFJCi4AUkSYqIQkQSoghodkVUcERRUUEG8igiAOOjoCMFVEsDIoK2AfkIaKOg6OIisr74Xuja9a89+bN/rXXPues852zzwfACAyWSDNRNYAMqUIeEeCDx8TG4eQuQIEKJHBcMBAIs2Qhc/0jAVww+H48PCtcIsAHvlwwAXjTCwhcMMBNm8AwHIf/D+pCmVxcAYCEAcB0kThLCIAUXDBAeo5CplwwQEYBgJ2YJlNcMKAEXDBgy2Ni41wwUC1cMGBcJ3/m01wwgJ34mXsBXDBblCEVAaCRXDAgE2WIRFwwaDtcMKzPVopFXDBY...6B2flQRKMpIP+DXCJ7HsLarL6Op8HdHo/cKIqsCFA4DsBDRQIS30pzU8aoX9JqqWYOntZjRPR6URKyCQVTCOGPXDDMiu8BWVM0P8z8cAzyOrJpdSgciP8+C8AX4VVUgLKOiHrSGqWC6QdFLGWnXDDsXCeivfHIulwiVdcZGHyV9mig3UVcMJKeKGdE9F/oUmo/Dlww+FmRXhaI6N+6UAzZjx8x875m3kOYV2AIXCJa34ViiLYU1XDWiO/o4jBEm9P9GoWpLFwwfUVV7o1CG/J8UW1nbUi5Qy85GdUkXCLPEtFzhQJcIlwiIFwiiMh1XCKyehwb863MfGHeNWQN25BDdoEyX4nB4+tmmtkUNHdwFxFRPxH93MymmNkxRPQGEf23mR0Xq82tfRqHXDDgpeiwnBj7tdvM2MzSVL5l7n+TmXcDWGdmvYWqy/FSWP3/Rkq7Q9AFpEtdQLqAdKkLyNig/xtcMETk0l30p0FqXDBcMFwwXDBJRU5ErkJggg==">
How about if you store the location of the image in DB then store the image itself in the file system? It's a better choice I think.
Proper way to display the blob images stored in DB is
echo '<img src="data:image/jpeg;base64,' . base64_encode($row['img']) . '">';
how to display image in html by retrieving it from mysql database.
i have the following code but only text is being displayed in place of image.
How to display the image instead of the text of image.
Code:
while ($row = mysql_fetch_assoc($result))
{
//echo "<div>";
//echo "<div class=\"slide-image\">";
print $row['Image'];
//echo "</div>";
echo "<div class=\"slide-text\">";
echo $row['Head'];
echo $row['Description'];
//echo "</div>";
//echo "</div>";
}
It displays the resulting JPEG as text, which as you might imagine is very difficult to interpret.
You have to set the Content-Type header with PHP. If it is JPEG it will be image/jpeg. You can set it with:
header('Content-Type: image/jpeg');
Be sure to set that header before outputting any data.
update
But in your file as you are outputting image data and text data from same document you cant set its header type as multiple types.
one option would be
while ($row = mysql_fetch_assoc($result))
{
echo "<div>";
echo "<div class=\"slide-image\">";
echo "<img src='image_render.php?id=".$row['some_key_for_record']."'";
//print $row['Image']; we are moving it to an external page
echo "</div>";
echo "<div class=\"slide-text\">";
echo $row['Head'];
echo $row['Description'];
echo "</div>";
echo "</div>";
}
and in image_render.php
//code to pull data from db according to $_GET['id'];
header('Content-Type: image/jpeg');
print $row['Image'];
//no text data to be output from here
If you are stroing the image source(full path) in your MYSQL table then you can able to print the html img tag inside your php like a html div. You can do it by this way,
while ($row = mysql_fetch_assoc($result))
{
//echo "<div>";
//echo "<div class=\"slide-image\">";
echo '<img src='".$row['Image']."' width="100" height="100" />';
//echo "</div>";
echo "<div class=\"slide-text\">";
echo $row['Head'];
echo $row['Description'];
//echo "</div>";
//echo "</div>";
}
Note :
If you want only the image to be displayed in your web page then you can do it with header('Content-Type: image/jpeg');
I have stored images path in database and images in project folder. Now i am trying to view all IMAGES with their PRODUCTS in my HTML template. I have written the following code and its given me an empty images box in the output and when i open that image box in the new tab, it opens the following URL.
http://localhost/cms/1
Kindly tell me the way of referring images in 'img src ' tag.
include 'connect.php';
$image_query = "select * from product_images";
$image_query_run = mysql_query($image_query);
$image_query_fetch = mysql_fetch_array($image_query_run);
if (!$query=mysql_query ("select product_id,name from products")) {
echo mysql_error();
} else {
while ($query_array = mysql_fetch_array($query)) {
echo '</br><pre>';
$product_id = $query_array['product_id'];
echo "<a href='single_product.php?product_id=$product_id' >";
print_r ($query_array['name']);
echo "</a>";
echo "<img src=". $image_query_fetch
['images'] ." alt=\"\" />";
echo '</pre>';
}
}
} else {
echo "You need to Log in to visit this Page";
}
Add your local image path before source
example
echo "<img src='http://localhost/cms/1/". $image_query_fetch['images'] .'" alt=\"\" />";
*Use PHP *
<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>
You can use the HTML base tag to set a base URL that relative URLs will be resolved from.
See -> http://www.w3schools.com/tags/tag_base.asp
I'm new to php. I have a sample mysql db in that I have a table named testdb with columns id(INT) and image(BLOB). I have uploaded an image into testdb. Uploaded successfully. The following is the php code. The variable $conn contains the connection details. I have a html page which redirects to this php page on submitting.
<?php
$name = $_FILES["sample"]["name"];
echo $name . "<br/>";
$tmp_name = $_FILES["sample"]["tmp_name"];
echo $tmp_name . "<br/>";
$size = $_FILES["sample"]["size"];
echo $size . "<br/>";
$contents = file_get_contents($tmp_name);
$htmlen = htmlentities($contents);
$cont = mysql_real_escape_string($contents);
$query = "INSERT INTO testdb(image)
VALUES ('$cont')";
$dbquery = mysql_query($query, $conn);
if($dbquery){
echo "successfully inserted";
}
else{
echo "could not inserted" . mysql_error();
}
?>
I am trying to get the image with the following code. But it is showing string characters rather than the image. As far as I know this should work fine.
<?php
$query = "SELECT image, id
FROM testdb ";
$dbquery=mysql_query($query , $conn);
if(! $dbquery){
echo "Could not selected the data from database. " . mysql_error();
}
while( $row = mysql_fetch_array($dbquery) ){
$decodeimg = html_entity_decode($row["image"]);
echo "<img src= $decodeimg/><br/> hellow orld <br/>";
}
?>
Could anyone help me with this. Thanks in advance.
Instead of storing the actual image in your database (which is redundant because it is probably stored on your server too); why don't you just store the PATH to the image as a string, query the string from your db and then append it to the 'src' attribute with php.
I am also got the same error when show the BLOB image from DB. I just use the decoding method for this problem....
$photo=$myrow['image'];
echo '<img src="data:image/jpeg;base64,' . base64_encode( $photo ) . '" width="150" height="150" />