How to echo images based on data from database? (Mysql, Php) - php

I am doing some sort of online storefront and each item has a corresponding image in the database. i need to echo the image of that specific item, how do i do it?
This is what i've done, But it doesn't seem to work:
<?php
$prebuy = "SELECT lot_image FROM lots WHERE lot_id= '$lot_id'";
$prebuyres = mysqli_query($mysqli, $prebuy) or die(mysqli_error($mysqli));
$lot_name = mysqli_fetch_assoc($prebuyres);
?>
<img src="C:\\xampp\htdocs\storefront\img\<?php echo ucwords($lot_name['lot_image']); ?>"
The image does not appear but there is no error either. What am i doing wrong? Please, help if you can.
Thanks in advance!

You missed enclosing img tag. try this way...and what about ucwords, to be sure first check result before using it.
echo ucwords($lot_name['lot_image']);
<img src="C:\\xampp\htdocs\storefront\img\<?=ucwords($lot_name['lot_image'])?>"/>
Edit:
set you image source relative to your php script in server like
<img src="img/<?=ucwords($lot_name['lot_image']);?>"/>

Related

Use a PHP variable to dynamically display an image in HTML

I'm trying to use a variable in a .php file to choose img src.
Here's my attempt:
<?php echo "<img src=\"$ICON\">"; ?>
or
<img src="<?php echo $ICON; ?>"/>
Neither have worked, is there anything obviously wrong? There's nothing displaying, not even the code, when I render the page.
Any guidance would be much appreciated, especially a correct implementation of the above. Sorry for poor code, only looked at these languages since yesterday.

Retrieving image file location from database not displaying

Ok, so here is what I am looking to do...I want to be able to upload a picture to the specified folder, and the path is inserted into the database(That part is done)
Then I need to pull the images from the database and display them on the index.php page. I know it's a simple process but I must be missing something.(grrr) I just can't seem to get the image to display on the index.php page, I can display the file location on getimage.php and echo out the image, but can't seem to reference the location to the img src and get it to display on index.php
Here is the code for the index.php page:
I want to be able to pull the image from getimage.php and display the image on the index.php page.
<a href="gallery.html">
<img src="getimage.php?id=18" alt="" width="200" height="133" /></a>
Here is the getimage.php page:
<?php
include("connect.php");
$id = mysql_real_escape_string($_GET['id']);
$sql = mysql_query("SELECT * FROM pictures WHERE p_id='$id'");
while($row = mysql_fetch_array($sql))
{
header("content-type :image/jpeg");
$image = $row['image'];
echo $image;
}
?>
So what this script is going to do is:
On the index page there are 3 pictures, there will be preset slots in the database for them. The user will be able to change these 3 photos anytime by uploading another photo (the upload script will update the old ones with the new ones.) the pictures will be displayed by their ID.
I could simply run 3 queries, 1 for each ID but I was thinking maybe i'm missing an easier way to do it.
Help is very much appreciated...Thank you.
(Yes I am aware of SQL injection as well as deprecation)
Your code is absolutely not correct correct. You have to change the line to :
echo '<img src="data:image/jpeg;base64,'
.base64_encode($image['file_data']).'" width=300 height=200/>";

Doesn't show blob image in php from mysql db

Can someone help me with this code? The problem is it doesn't show the image but this weird stuff: OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOÿÀx !ÿÄ¢  }!1AQa"q2‘¡#B±ÁRÑð$3br‚%&'()*456789:CDEFGHIJSTUVW
Here is my code:
<?php
//mysql connect database
mysql_connect('localhost','root','password') or die(mysql_error());
mysql_select_db('database') or die(mysql_error());
$res=mysql_query('select * from img');
while($row=mysql_fetch_array($res))
{
echo "<div id='image'>";
echo "<p/>";?> <img src="<?php echo $row['image'];?>" height='300px' width='468px'>";<?php
echo "<p/>"; echo $row['name'];
echo "</div>";
}
?>
<img> expects a URL pointing at where the image file is. You're trying to dump the raw binary garbage of the image into that image tag. That garbage will naturally contain " and > chars in it, and "close" your img tag, letting the rest of the garbage be treated as plain text.
You either have to serve up your image via separate script, e.g.
html:
<img src="pic.php?id=foo">
php:
header('Content-type: image/jpeg');
echo get_blob_from_database($_GET['id']);
Or embed the image inside the html properly:
<img src="data:image/jpg;base64,<?php echo base64_encode($row['image']); ?>">
And neither of these is particularly a good solution. Storing images directly in the database is almost never a good idea.
it's better to save your images in folder rather than save in blob format
you can save the file name in database table and save the images in folder
when you call the filename, you can combine it with src from and get the content

Can i get a table name and turn it into a variable then put it in a img tag

I'm trying to make a vote thing for my Minecraft server. I want it to show
on there as in the site. The database is MySQL.
Here is my code. It's just a test; I'm just trying to turn a link into a variable.
<?php
$imageNumber = https://minotar.net/helm/GRANTSWIM4/100.png;
?>
<img src="<?php echo $imageNumber ?>.jpg">
The code you provided will work fine, assuming you fix the syntax issues and remove the double extension (you are appending .jpg after the .png).
<?php $imageNumber = 'https://minotar.net/helm/GRANTSWIM4/100.png'; ?>
<img src="<?php echo $imageNumber ?>">
If you are asking how to get a MySQL table name and reference the value in a variable, then yes. You can retrieve the table name from the result with mysql_tablename or get the full list of tables with mysql_list_tables methods.
But I wouldn't suggest you to include your table names on the client side.

Creating an Image from a Blob

I'd like to store some photos in MySql as blobs and would like to be able to retrieve the binary and recreate images from it to display back to the user.
Everything I've search uses some combination of file_get_contents, Base 64 encoding and the GD library. But every post I come across has a different requirement than what I'm trying to do and so the code examples aren't that helpful.
Can someone tell me what function calls I need to make or give me an order of operations for what needs to be done. Say I have the following code:
$someBlob = getImageBlobByImageId(1203);
Say this blob represents an image named "foo.jpg". How do I go from $someBlob to foo.jpg so I could put it in HTML like
<img src="<?php echo $fooImage; ?>"/>
Any hints or nudges in the right direction are greatly appreciated ;-)
Your HTML needs to point to a PHP script that retrieves the blob and sends it back on request as an image.
For example, in your HTML:
<img src="getImage.php?id=1203" />
And then, for getImage.php:
<?php
$id = $_REQUEST['id'];
header("Content-type: image/jpg");
echo getImageBlobByImageId($id);
exit();
?>
Be sure not to have the script output any content other than the header line and the blob data, or you won't get what you want.

Categories