0

I want to fetch image from database to html table cell. I have tried something but it gives me an error. Let me know what is wrong with this code.

         <tbody>
          <?php
         $result = mysqli_query($conn,$sql);
         while($row = mysqli_fetch_array($result,MYSQLI_BOTH)) {  
         echo " <tr>
         <th>1</th>

         <td>{$row['itemId']}</td>
        **<td><img src="imageView.php?image_id=<?php echo .$row["imageId"].; ?   
         >" /><br/>**
        <td/>
         <br/>
         </td>
         <td>{$row['itemName']}</td>
         </tr>";
         }
        ?>
       <tr>
      <th scope="row">2</th>
      <td>Nicholas</td>
       <td>Sanchez</td>
     <td>username</td>
     </tr>
     <tr>
    <th scope="row">3</th>
      <td>Debra</td>
     <td>Shaw</td>
   <td>username</td>
   </tr>
  <?php 

  mysqli_close($conn);
 ?>
</tbody>

2 Answers2

2

You are trying to use the PHP tag inside echo "";

Instead of

<?php echo .$row["imageId"].; ?>

Try this

{$row["imageId"]}

Or

".$row["imageId"]."

Also don't forget to escape the image tag. Don't use double-quotes if your echo contains double-quotes too. Use single quote for the image tag instead '. If you wish to use double quotes for your image tag, escape it like this

<img src=\"imageView.php?image_id=".$row["imageId"]."\" />

But to ensure you're safe with the quotes in your image tag, you could use the single quotes only:

<img src='imageView.php?image_id=".$row["imageId"]."' />
node_modules
  • 4,790
  • 6
  • 21
  • 37
0
<tbody>
<?php
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH))
{
    echo '
<tr>
    <th>1</th>
    <td>' . $row['itemId'] . '</td>
    <td><img src="imageView.php?image_id=' . $row["imageId"] . '>" /></td>
    <td>' . $row['itemName'] . '</td>
</tr>
';
    }
?>
<tr>
    <th scope="row">2</th>
    <td>Nicholas</td>
    <td>Sanchez</td>
    <td>username</td>
</tr>
<tr>
    <th scope="row">3</th>
    <td>Debra</td>
    <td>Shaw</td>
    <td>username</td>
</tr>
<?php mysqli_close($conn); ?>
</tbody>

If you use HTML in a string use single quotes to surround it. You also shouldn't use opening and closing PHP tags within PHP code. In addition to that you had malformed HTML in your code:

<br/>**
    <td/>
     <br/>

You don't need to put a br /> between two cells.

Dan
  • 5,140
  • 2
  • 15
  • 30
  • Thank You. It helped. One more thing can you tell me what is the best way to use width and height attributes inside this same image tag. –  Jul 31 '17 at 18:09
  • You can either use the `height` and `width` attribute or just format the tag with the `style`-attribute and some CSS (`height` and `width` to ignore the image proportions, `max-width` and `max-height` to scale it down proportional). – Dan Jul 31 '17 at 18:14
  • This gives me error. –  Jul 31 '17 at 18:16
  • Which error message do you get? – Dan Jul 31 '17 at 18:17
  • Parse error: syntax error, unexpected '90' (T_LNUMBER), expecting ',' or ';' in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\drop2\component-tables - Copy.php on line 1378 –  Jul 31 '17 at 18:18
  • You're still mixing `'` and `"` within a string literal. If you want to use the same character you used to specify the string you have to escape it with a backslash, like this: `echo 'this wouldn\'t break the string.';` ... and the closing tag begins with the slash: `` instead of ``. – Dan Jul 31 '17 at 18:21
  • Thanx @Spingolini Now i got my mistake –  Aug 01 '17 at 04:58