Home > Software design >  images appearing in warning but not in table?
images appearing in warning but not in table?

Time:07-20

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

CodePudding user response:

<?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);
?>

CodePudding user response:

The fact that is showing the image in the warning is because you're using a tag with the source as an array key which is not correct. The array keys, so what is inside the square bracket, is the reference to the array position. If you're familiar with C for example is the 0, 1, ecc.. and not the value itself.

CodePudding user response:

Yes as @NigelRen mentioned this row $row['<img src="data:image/jpeg; looks very bad. I think you should use:

echo "<td><img src='data:image/jpeg;base64," . base64_encode($row['advertiser_logo']) . "'/></td>";
  • Related