Search code examples
phpimagewarnings

images appearing in warning but not in table?


I am working on a website whereby a load of advertisers are stored in the DB and then displayed to the user by there logo. I know storing directly in to the DB for images is not the done thing, however, I am starting out this way, to get the website running and then will refactor to move to a much more suitable approach.

Currently, I have the following PHP code:

    <?php

session_start();
require_once "config.php";
 
// Create connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$sql = "SELECT * FROM advertisers";
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
        echo "<table>";
            echo "<tr>";
                echo "<th>id</th>";
                echo "<th>advertiser_Name</th>";
                echo "<th>advertiser_URL</th>";
                echo "<th>advertiser_Category</th>";
                echo "<th>advertiser_logo</th>";
            echo "</tr>";
        while($row = mysqli_fetch_array($result)){
            echo "<tr>";
                echo "<td>" . $row['advertiser_id'] . "</td>";
                echo "<td>" . $row['advertiser_Name'] . "</td>";
                echo "<td>" . $row['advertiser_URL'] . "</td>";
                echo "<td>" . $row['advertiser_Category'] . "</td>";
                echo "<td>" . $row['<img src="data:image/jpeg;base64,'.base64_encode($row['advertiser_logo']).'"/>'] . "</td>";

            echo "</tr>";
        }
        echo "</table>";
        // Free result set
        mysqli_free_result($result);
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>

However, the images are displayed when called from the DB but they are displayed in the warning message rather than in the table? Image of Images in warning not in table


Solution

  • <?php
    session_start();
    require_once "config.php";
    
    // Create connection
    if ($link === false)
    {
        die("ERROR: Could not connect. " . mysqli_connect_error());
    }
    
    $sql = "SELECT * FROM advertisers";
    if ($result = mysqli_query($link, $sql))
    {
        if (mysqli_num_rows($result) > 0)
        {
            echo "<table>";
            echo "<tr>";
            echo "<th>id</th>";
            echo "<th>advertiser_Name</th>";
            echo "<th>advertiser_URL</th>";
            echo "<th>advertiser_Category</th>";
            echo "<th>advertiser_logo</th>";
            echo "</tr>";
            while ($row = mysqli_fetch_array($result))
            {
                echo "<tr>";
                echo "<td>" . $row['advertiser_id'] . "</td>";
                echo "<td>" . $row['advertiser_Name'] . "</td>";
                echo "<td>" . $row['advertiser_URL'] . "</td>";
                echo "<td>" . $row['advertiser_Category'] . "</td>";
                echo "<td><img src='data:image/jpeg;base64," . base64_encode($row['advertiser_logo']) . "'/></td>";
    
                echo "</tr>";
            }
            echo "</table>";
            // Free result set
            mysqli_free_result($result);
        }
        else
        {
            echo "No records matching your query were found.";
        }
    }
    else
    {
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }
    mysqli_close($link);
    ?>