Display image using php with <img src=""

41,696

Solution 1

Don't echo large blocks of HTML in PHP like that. Its bad practice. No, actually, its horrible practice. Instead learn to open and close the PHP tag as needed, like:

<?php
//code..code...code...
while($row = mysqli_fetch_array($result))
{
?>                        
  <tr>      
    <td> <?php echo $row['x']; ?> </td>
    <td> <?php echo $row['y']; ?> </td>
    <td> <?php echo $row['z']; ?> </td>
    <td> <?php echo $row['f']; ?> </td>
    <td> <?php echo $row['g']; ?> </td>
    <td> <?php echo $row['d']; ?> </td>
    <td><img src="<?php echo $url; ?>"/></td>
  </tr>
<?php
}
//code..code...code
?>

There are several benefits to this, including that its less likely to break syntax highlighting and your code is not defaced with as many \" all over the place.

Solution 2

You're already inside PHP - you shouldn't open another <?php scope:

  echo ' 
      <tr>      
      <td> '.$row['x'].' </td>
      <td> '.$row['y'].' </td>
      <td> '.$row['z'].' </td>
      <td> '.$row['f'].' </td>
      <td> '.$row['g'].' </td>
      <td> '.$row['d'].' </td>
      <td><img src="' .$url . '"/></td>
      </tr>';
Share:
41,696
Akinn
Author by

Akinn

Updated on January 09, 2020

Comments

  • Akinn
    Akinn over 4 years

    I have stupid problem with the html/php rules. I'm trying to show an image from an apache server with this code using a table:

    <?php
    
        //code
    
        while($row = mysqli_fetch_array($result)) {
    
            echo ' 
                <tr>        
                  <td> '.$row['x'].' </td>
                  <td> '.$row['y'].' </td>
                  <td> '.$row['z'].' </td>
                  <td> '.$row['f'].' </td>
                  <td> '.$row['g'].' </td>
                  <td> '.$row['d'].' </td>
                  <td><img src=\"<?php echo $url; ?>\"/></td>
                </tr>';
    
    }
    
    //code
    
    ?>
    

    But obviusly the inner php script is considered as normal text and no run!