skip to Main Content

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 class="gallery"> 
        <?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 class="status error">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

2

Answers


  1. 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 class="gallery"> 
            <?php while($row = $result->fetch_assoc()): ?> 
                <img src="data:image/jpg;charset=utf8;base64,<?= base64_encode($row['image']); ?>" /> 
            <?php endwhile; ?> 
        </div> 
    
        <?php else: ?>
            <p class="status error">Image(s) not found...</p> 
    
    <?php endif; ?>
    
    Login or Signup to reply.
  2. 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 class="gallery"> 
            <?php 
            while ($row = $result->fetch_assoc()){ 
                echo '<img src="data:image/jpg;charset=utf8;base64,'.base64_encode($row['image']).'" />';
            } 
            ?> 
        </div> 
    <?php }else{ ?> 
        <p class="status error">Image(s) not found...</p> 
    <?php } ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search