I have been trying to echo images from my database using PHP. I have tried various solutions on the forum with no success. I am just getting a blank image as an output.
while($rows=mysqli_fetch_assoc($getquery))
{
$id=$rows['id'];
$LastName=$rows['LastName'];
$FirstName=$rows['FirstName'];
$Year=$rows['Year'];
$Press=$rows['Press'];
$Description=$rows['Description'];
$Title=$rows['Title'];
$image=$rows['image'];
echo '<div class = "paragraph">' . $LastName . '<br/>'. $FirstName . '<br/>' . $Year . '<br/>' . $Press . '<br/>' . $Title . '<br/>'. $Description . "<img src='image/".$row['image']."' />" . '</div>' ;
}
You're echoing the image as $row['image'], but the only prior references to the image are $rows['image'] (note the s) and $image. Update the echo statement to use either of these and not $row['image'].
Edit: This will not fully fix your issue. As noted in a comment to your question, you will need to do some finagling to actually display the image as demonstrated here.
while($rows=mysqli_fetch_assoc($getquery)){
$id=$rows['id'];
/*
assuming the `image` directory is relative to the root
and not the directory from which the script runs then
a leading slash might be the issue.
*/
echo "
<div id='{$id}' class='paragraph'>
{$rows['LastName']}<br/>
{$rows['FirstName']}<br/>
{$rows['Year']}<br/>
{$rows['Press']}<br/>
{$rows['Title']}<br/>
{$rows['Description']}
<!--<img src='/image/{$row['image']}' />-->
<img src='data:image/jpeg;base64, {$rows['image']}' alt='' title='' />
</div>";
}
If you store the mimetype of the image when you save it to the db then you would substitute the proper mime/content type in above ~ ie: image/png, image/gif etc so that might then become something like:
<img src='data:{$rows['mimetype']};base64, {$rows['image']}' alt='' title='' />
Related
I'm trying to get this so when I click the image, the image then shows in a new tab so it's able to be saved
I've tried adding the a href tags inside and around.
Code:
echo "<img src=" . random_image('css/avatars') . " />";
This is what you want:
echo '<img src="' . random_image('css/avatars') . '" />';
You should try
echo "<a target='_blank' href='".random_image('css/avatars')."'>"."<img src=" . random_image('css/avatars') . " /></a>";
I think this can help you
The problem is that you are not properly concatenating your string with what you get from random_image function.
You can try following code, here I am assuming that your random_image function returns complete path to your image
function random_image($path)
{
// returning dummmy image
return "https://images.unsplash.com/photo-1535498730771-e735b998cd64?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=623ed05afcc5a3fa1e444ffca95854dc&w=1000&q=80";
}
echo "<a href='".random_image('css/avatars')."' target='_blank'> <img src='".random_image('css/avatars')."' /></a>";
I know this has already been put out on here but I've looked and mine still won't work and I don't know why someone please give me a hand.
echo '<img src="' . Mage::helper('catalog/image')->init($_product, 'small_image')->resize(50,50); . '"/>';
Remove the semicolon you have after resize
echo '<img src="' . Mage::helper('catalog/image')->init($_product, 'small_image')->resize(50,50) . '"/>';
Try this
echo "<img src= '".Mage::helper('catalog/image')->init($_product, 'small_image')->resize(50,50)."'/>";
Please change
echo '<img src="' . Mage::helper('catalog/image')->init($_product, 'small_image')->resize(50,50); . '"/>';
this line to
echo '<img src="' . Mage::helper('catalog/image')->init($_product, 'small_image')->resize(50,50) . '"/>';
Because you had put one extra semi column ; after the resize(50,50)
Please change your code and try. Definitely work .
can anyone tell my why this:
echo "<a href='productdetail.php?product=" . $rij['productnaam'] . "'><img src='../images/producten/monster_energy_green.png' alt='' /></a>";
does give a clickable image link, and this:
echo "<a href='productdetail.php?product=" . $rij['productnaam'] . "'><img src='" . $rij['afbeelding_klein'] . "' alt'productafbeelding' /></a>";
doesnt? (not clickable).
thanks in advance!!
php rookie..
In the first one you have a real scr it's directing to with a file extension. the second does not. If the second is supposed to be picking up a variable we would need more information
echo "<a href='productdetail.php?product=" . $rij['productnaam'] . "'><img src='" . $rij['afbeelding_klein'] . "' alt='productafbeelding' /></a>";
just add a "=" between alt and 'productafbeelding'
Try this one:
echo '<img src="' . $rij['afbeelding_klein'] . '" alt="productafbeelding" />';
Screenshot of the code:
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
require ('../../mysqli_oop_connect.php'); //Connect to the database.
$errors = array(); // Initialize an error array.
// Check for an isbn:
if (empty($_GET['isbn'])) {
$errors[] = 'You forgot to enter the ISBN-13.';
} else {
$isbn = $mysqli->real_escape_string(trim($_GET['isbn']));
}
if (empty($errors)) {
//Make the query:
$q = "SELECT * FROM books WHERE isbn=$isbn";
$r = $mysqli->query($q);
$num = $r->num_rows;
if ($num == 0) { // If it fails to run:
echo "<br /><br /><p>This book is currently not listed in our database. Would you like to add it?</p>\n";
//HERE IS WHERE IT NEEDS TO REDIRECT TO A PAGE THAT ALLOWS THE CONTENT TO BE ADDED TO THE DB LIKE REGISTER.PHP DOES.
} else { //If it suceeds then run the query:
echo '<h1>Is this your book?</h1>';
while ($row = $r->fetch_object()) { // POST DATA FROM DB.
echo '<img src="$row->image" height="100" width="100">' . '<br />' . $row->name . '<br />' . $row->isbn ;
The rest of the code is just closing the DB connection.
I don't know what that icon means or where the issue is in the code. The DB is setup with three columns in a table: image, isbn, and name. The goal is to get the book to display the data being retrieved from the database. It is displaying all three items from the database based on the query, except the image is something else. The HTML is recognizing that there is an image being retrieved because when I just do $row->image it freaks out and posts random nonsense, as it should when there is an image being displayed like that. Any help is greatly appreciated.
echo '<img src="'.$row->image.'" height="100" width="100"><br />' . $row->name . '<br />' . $row->isbn ;
Try this way, and better you print your image path and check if it does exist or does not exist, and yes you can also inspect image element and check path of the image.
Its not clear in the question whether there is image link saved in the db or image data in base64. Answer is given assuming db has image link.
I'd guess from your comments about 'freaks out and posts random nonsense' that your image is stored in your database, rather than the path to your image. If so, you can't simply echo the image data in your <img> tag. The src attribute requires a URL, which you can create in one of two ways: create a script that serves the image with appropriate headers and use the URL of that script as your src attribute; or, create a data URI and include the data in your page. The latter is probably the simplest, but could increase the load time of your pages. Here's how:
// base64 encode image data and prepend the header
$imgURL = "data:image/png;base64,".base64_encode($row->image);
echo '<img src="'.$imgURL.'" height="100" width="100">' .
'<br />' . $row->name . '<br />' . $row->isbn ;
Note - you'll need to change the dataURL header to send the correct image type: image/jpg, image/png, etc.
You cannot have string substitution with single quotes. It will simply place the text $row->image in the echo:
echo '<img src="$row->image" height="100" width="100">' . '<br />' . $row->name . '<br />' . $row->isbn ;
Try this instead:
echo "<img src='$row->image' height='100' width='100'>" . '<br />' . $row->name . '<br />' . $row->isbn ;
Or this:
echo '<img src="' . $row->image . '" height="100" width="100">' . '<br />' . $row->name . '<br />' . $row->isbn ;
Personally, I prefer using sprintf like this:
echo sprintf('<img src="%s" height="100" width="100">', $row->image) . '<br />' . $row->name . '<br />' . $row->isbn ;
But on top of that, to make your debugging life even easier, I recommend formatting your PHP code to be easier to read like this:
echo sprintf('<img src="%s" height="100" width="100">', $row->image)
. '<br />'
. $row->name
. '<br />'
. $row->isbn
;
The cleaner your code is from the beginning, the easier it is to spot flaws & bugs when they arise.
I have this code:
if ($topic['user']==$_SESSION['display'])
{
echo '<div class="bottomright"><img src="../assets/icons/Comments-edit.png" /><img src="../assets/icons/Lock.png" /><img src="../assets/icons/Trash.png" /></div>';
}
When I hove over the image the link shows as:
?id=%3C?php%20echo%20$topic_id;%20?%3E&part=3
Rather than:
?id=3&part=3
Why is this not working?
I tried this:
if ($topic['user']==$_SESSION['display'])
{
echo '<div class="bottomright"><img src="../assets/icons/Comments-edit.png" /><img src="../assets/icons/Lock.png" /><img src="../assets/icons/Trash.png" /></div>';
}
Now I get:
?id=$topic_id&part=3
Your code is already within PHP tags, you don't have to add an additional <?php to the link's parameters.
echo '<div class="bottomright"><a href="'. $topic_id .'&part=5"...
Simply terminate your string and concatenate the relevant variables into the string.
Here is a very simplified example:
<?php
$user_name = "Anthony";
echo "Hello ". $user_name ."! How are you?";
//----------^ terminating the string
?>
This will result in:
Hello Anthony! How are you?
Here is the relevant documentation dealing with string concatenation.
You are echoing a php statement, it won't be interpreted, but displayed as it is. Instead, you should concatenate the string, like this:
if ($topic['user']==$_SESSION['display'])
{
echo '<div class="bottomright"><a href="?id='
. $topic_id
. '&part=5"><img src="../assets/icons/Comments-edit.png" /></a><a href="?id='
. $topic_id
. '&part=6"><img src="../assets/icons/Lock.png" /></a><a href="?id='
. $topic_id
. '&part=7"><img src="../assets/icons/Trash.png" /></a></div>';
}
You don't need to "echo" within a PHP statement. Just concatenate the string (here id) as shown here:
if ($topic['user']==$_SESSION['display']){ echo '<div class="bottomright"><img src="../assets/icons/Comments-edit.png" /><img src="../assets/icons/Lock.png" /><img src="../assets/icons/Trash.png" /></div>'; }
Also, you should use "& amp;" (without that space, don't know how to format that here) instead of "&" in your links.