0

This is part of a small project that im working, in this page I getting info from a database, my question(problem) is why this line of code works.

echo '<td><a href="add.php?id=' .$row['id'] .'">' . htmlentities($row['name'], ENT_QUOTES, 'UTF-8') . '</a></td>';

But this one don't work

<td><?php ' ' . htmlentities($row['name'], ENT_QUOTES, 'UTF-8') . ' ' ?> </td>

This is the entire code of the page

 <?php
    require("coneccion.php");
    $id = $_GET['id']; 
    $_SESSION['id'] = $id;



    $query = "SELECT id, name, due, points FROM grades WHERE id = '$id' ";

    try
    {
      $stmt = $db->prepare($query);
      $stmt->execute();
    }
    catch(PDOException $ex)
    {
      die("Error 1" . $ex->getMessage() );
    }

    $rows = $stmt->fetchAll();

    ?>



    <br />
    <a href="add.php">Add</a>  

    <table border="1">
      <tr>
        <th>Name</th>
        <th>Due</th>
        <th>Points</th>
      </tr>
      <?php foreach($rows as $row):
      echo "<tr>";
         echo '<td><a href="add.php?id=' .$row['id'] .'">' . htmlentities($row['name'], ENT_QUOTES, 'UTF-8') . '</a></td>'; 
         ?> 
          <td><?php ' ' . htmlentities($row['name'], ENT_QUOTES, 'UTF-8') . ' ' ?> </td>
          </tr>
         <?php

         endforeach;
1
  • you need to fire a command which will allow php to generate an output like echo, print. <td><?php ' ' . htmlentities($row['name'], ENT_QUOTES, 'UTF-8') . ' ' ?> </td> can be corrected as <td><?php echo(htmlentities($row['name'], ENT_QUOTES, 'UTF-8')); ?> </td> Commented Apr 8, 2014 at 21:30

2 Answers 2

1

Instead of::

<td><?php ' ' . htmlentities($row['name'], ENT_QUOTES, 'UTF-8') . ' ' ?> </td>

try

 <td><?php echo ' ' . htmlentities($row['name'], ENT_QUOTES, 'UTF-8') . ' '; ?> </td>

Notice the echo statement in there, so we can output the content back to the user.

Sign up to request clarification or add additional context in comments.

Comments

0

Because the characters ' ' . cannot be interpreted by PHP without any command like echo. If you look at the 2nd line like this it will be clear maybe:

<td>
<?php
    ' ' .    // PHP cannot parse this
    htmlentities($row['name'], ENT_QUOTES, 'UTF-8');    // PHP can parse this
    . ' '    // PHP cannot parse this
?>
</td>

The correct way would be:

<td>
<?php
    echo ' ';    // PHP cannot parse this
    echo htmlentities($row['name'], ENT_QUOTES, 'UTF-8');    // PHP can parse this
    echo ' ';    // PHP cannot parse this
?>
</td>

or

<td>
<?php echo ' '.htmlentities($row['name'], ENT_QUOTES, 'UTF-8').' '; ?>
</td>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.