Returning SQL data within an image tag using PHP

how to retrieve image from database in php mysqli
how to fetch image from database in php and display in table
how to display an image in php from mysql database example
how to fetch image from database in php and display in div
how to display image from database in php - w3schools
how to display image from database in html using javascript
php display image from file
how to insert image in mysql database using php source code

When I run the following file I get the database data i.e it prints it out on the website so I know my connections are good.

<html>
<?php include 'config.php'?>
<?php include 'header.php'?>

<?php 
$sql = "SELECT name, image FROM images";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo $row["name"], $row["image"];
}
?>

</div>
</html>

However when I try and format the results like below

<html>
<?php include 'config.php'?>
<?php include 'header.php'?>

<?php 
$sql = "SELECT name, image FROM images";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo <div id = "bookbar">
<img src= "$row['image']" alt = "image">
<p> $row['name'] </p>

</div>
}
?>

</div>
</html>

it doesn't work. Can anyone help me fix the code?

Maybe try this your code didn't close/open the php tags properly also don't echo like that

<?php include 'config.php'?>
<?php include 'header.php'?>

<?php 

$sql = "SELECT name, image FROM images";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
       ?>

       <div id = "bookbar">
            <img src= "<?php echo $row['image'] ?>" alt = "image">
            <p><?php echo $row['name']; ?></p>
        </div>

       <?php
    }
}

$conn->close();
?>

If you want to echo something out

Its better to close on open the php tags like so

PHP goes here
?>
HTML goes here
<?php
PHP goes here

And if you want to echo something inside the HTML just do this

<span> <?php echo "something" ?> </span>

much easier and makes the code easier to read.

imagecreatefromstring - Manual, FALSE is returned if the image type is unsupported, the data is not in a recognised $image = imagecreatefromstring(file_get_contents($src)); Here’s how I did it. In this code, we will use two files – index.php and source.php and a database table with sample image data stored. Please note that I used BLOB data type for storing my images, it can handle up to 64KiB of data. If you want larger storage for each of your images, you can use LONG BLOB that can handle up to 2,048 KiB of data.

change your echo statement to -

echo '<div id = "bookbar"><img src= "' . $row['image'] . '" alt = "image"><p>'. $row['name'] .'</p>'

Constructing Usable Web Menus, o Chapter 5, PHP Example | - - | File Edit View Favorites Tools Help o *Back - - - 9 o If the site is particularly large and some pages are located in different directories, links instead of text, just by altering a line or two to write an 'img tag pointing to (for Trail Header in ASP with Microsoft SQL Server providing our data. Hi, I am not able to display a Image stored in Database using PHP. I have created a database, and in that I have created a table to store the Images of employees.

Your issue is a syntax problem - you can't use echo like that, it has to echo a string variable. You should be seeing an error message about it.

You could keep the echo statement and put all the HTML inside a string, and concatenate (or interpolate) the PHP data into it. But IMO the easiest thing here in terms of readability and maintenance is to step out of the PHP tags, print the HTML, embed some PHP tags in it for the variables, and then step back in again to continue with the code. It makes the HTML far easier to understand:

?>
<div id="bookbar">
  <img src="<?php echo $row['image'] ?>" alt="image">
  <p><?php echo $row['name'] ?></p>
</div>
<?php

PHP/FI Version 2.0 Documentation, If you do not understand what is being asked, simply hit return. The default choice phpSQLLogDB database: Set name of SQL database to use for logging. Default is "phpfi absolute URL's. In the above example if the image tag had been: On form submit, this PHP code gets the content of the image file and stores it into the MySQL BLOB column as binary data. Read Image BLOB to Display. For displaying BLOB images to the browser, we have to create a PHP file to do to following. get image data and type from MySQL BLOB

When you are in php mode you should echo strings as php variables wrapped with single quotes:

while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
    echo '<div id = "bookbar">';
    echo '<img src="' . $row['image'] . '" alt = "image">';
    echo '<p>' . $row['name'] . '</p>';
    echo '</div>';
}

imagepng - Manual, An image resource, returned by one of the image creation functions, such as If you're generating an image dynamically based on post data and don't want to to embed in: <img src="file.php?image=123.jpg[?maxX=200&maxY=150]"> (in the contents in, base64_encode() the string, and fire off your SQL query (use  First off you need to fetch the resulting row from the resultset of the query. For that you can use mysql_fetch_row.Now that you have the fetched row you can access the retrieved value and echo it into the src.

PHP mysqli fetch_field() Function, HTML Tag Reference HTML Browser Support HTML Event Reference HTML Color Return the next field (column) in the result-set, then print each field's name, table, $sql = "SELECT Lastname, Age FROM Persons ORDER BY Lastname"; db - database (new in PHP 5.3.6); catalog - catalog name, always "​def" (since  The pre tag can use child code, samp and kbd tags to create intelligent formatting. Data in SQL Server can easily generate this sort of structured HTML5 markup. This has obvious uses in indexes, chaptering, glossaries as well as the obvious generation of table-based reports. There is quite a lot of mileage in creating HTML from SQL Server queries

7 Loading Images, In a browser, enter the URL to access the Oracle Database Express Edition storing employee images, click the arrow on the SQL icon, highlight SQL Commands, <img> tag referencing the anyco_im.php file with the employee identifier as a RETURNING clause in the INSERT statement, its initial value is set to NULL . Returning values. Values are returned by using the optional return statement. Any type may be returned, including arrays and objects. This causes the function to end its execution immediately and pass control back to the line from which it was called.

Using JSON and generating a JPEG image, <?php /** * ac_get_json.php: Service returning equipment counts in JSON as cn from equipment group by equip_name"; $res = $db->execFetchAll($sql, "Get before the "<?php" tag because it will * be incorporated into the image stream The data is decoded from the JSON format and the array is sorted by name order. Tags for Photo Upload and Retrive with MySql using BLOB in PHP. images stroing in db; store the image interms of bytes; storing the image in db without the folders; insert and retrive image from database in php; insert image into mysql database using php code; insert images in mysql using php; php mysql blob; php mysql store images in database

Comments
  • Can you elaborate on "it doesn't work"? Are you getting an error? THe wrong result?