以下片段引起了
"PHP Parse错误:语法错误,/ Applications/MAMP/htdocs3/nettuts/PHP/PDO for Database Access/htdocs/view_users02.php第39行意外结束"
我环顾了网站和谷歌,但没有找到确切的解决方案.
foreach($DBH->query($sql) as $row){
$output = "<tr><td align='left'>" . $row["name"] . "</td><td align='left'>" . $row["dr"] . "</td></tr>";
// echo '<tr><td align="left">' . $row['name'] . '</td><td align="left">' . $row['dr'] . '</td></tr>';
echo <<<EOT
$output
EOT;
Run Code Online (Sandbox Code Playgroud)
完整的脚本
<?php
$page_title = 'View the Current Users';
include ('includes/header.html');
// Page header:
echo '<h1>Registered Users</h1>';
require_once ('../mysql_pdo_connect.php'); // Connect to the db.
// Make the query:
$sql = "SELECT CONCAT(last_name, ', ', first_name) AS name, DATE_FORMAT(registration_date, '%M %d, %Y') AS dr FROM users ORDER BY registration_date ASC";
// Table header.
echo <<<EOT
<table align='center' cellspacing='3' cellpadding='3' width='75%'>
<tr><td align='left'><b>Name</b></td><td align='left'><b>Date Registered</b></td></tr>
EOT;
foreach($DBH->query($sql) as $row){
$output = "<tr><td align='left'>" . $row["name"] . "</td><td align='left'>" . $row["dr"] . "</td></tr>";
// echo '<tr><td align="left">' . $row['name'] . '</td><td align="left">' . $row['dr'] . '</td></tr>';
echo <<<EOT
$output
EOT;
}
echo '</table>'; // Close the table.
$DBH = null;
include ('includes/footer.html');
?>
Run Code Online (Sandbox Code Playgroud)
EOT;在循环之后你有一大堆空格(确切地说是9个).
从手册
...在分号之前或之后可能没有任何空格或制表符......
为什么要将$output变量包装在HEREDOC字符串中?我只是将循环更改为
printf('<tr><td align="left">%s</td><td align="left">%s</td></tr>',
htmlspecialchars($row["name"]),
htmlspecialchars($row["dr"]));
Run Code Online (Sandbox Code Playgroud)
甚至更好,使用PHP的替代语法.
| 归档时间: |
|
| 查看次数: |
1461 次 |
| 最近记录: |