Home > Back-end >  My PHP tag is not working, the code editor is not registering it
My PHP tag is not working, the code editor is not registering it

Time:01-05

i have a php tag that is for some reason not working, when i first wrote the code it worked but after a while of me doing different things with the code it suddenly stopped working.

underneath here is my code

<?php 
// Include the database configuration file  
require_once 'config.php'; 
 
// Get image data from database 
$result = $db->query("SELECT billede FROM billeder ORDER BY ret_id DESC"); 
?>

<?php if($result->num_rows > 0){ ?> 
    <div > 
        <?php while($row = $result->fetch_assoc()){ ?> 
            <img src="data:image/jpg;charset=utf8;base64,<?php echo base64_encode($row['image']); ?>" /> 
        <?php } ?> 
    </div> 
<?php }else{ ?> 
    <p >Image(s) not found...</p> 
<?php } ?>

the problem is with this line. the question mark in the closing tag does not get registered as php.

<img src="data:image/jpg;charset=utf8;base64,<?php echo base64_encode($row['image']); ?>" /> 

I tried to go back but even then it still dit not work. its as if VS Code doesnt register it. does anyone know how to fix?

this is a picture of how it looks in my code editor VS Code

CodePudding user response:

One solution would be to echo the image tag, and then change it. So you have this line:

echo '<img src="data:image/jpg;charset=utf8;base64,'.base64_encode($row['image']).'" />';

So in your code it would be:

<?php 
// Include the database configuration file  
require_once 'config.php'; 
 
// Get image data from database 
$result = $db->query("SELECT billede FROM billeder ORDER BY ret_id DESC"); 
?>

<?php if($result->num_rows > 0){ ?> 
    <div > 
        <?php 
        while ($row = $result->fetch_assoc()){ 
            echo '<img src="data:image/jpg;charset=utf8;base64,'.base64_encode($row['image']).'" />';
        } 
        ?> 
    </div> 
<?php }else{ ?> 
    <p >Image(s) not found...</p> 
<?php } ?>

CodePudding user response:

Use this Approach. Much Neater. Should have you sorted

<?php 
// Include the database configuration file  
require_once 'config.php'; 
 
// Get image data from database 
$result = $db->query("SELECT billede FROM billeder ORDER BY ret_id DESC"); 
?>

<?php if($result->num_rows > 0): ?> 

    <div > 
        <?php while($row = $result->fetch_assoc()): ?> 
            <img src="data:image/jpg;charset=utf8;base64,<?= base64_encode($row['image']); ?>" /> 
        <?php endwhile; ?> 
    </div> 

    <?php else: ?>
        <p >Image(s) not found...</p> 

<?php endif; ?>
  • Related