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;
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.
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>