我试图在PHP中显示一个表.我已经建立了有效的连接.我收到错误:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /Applications/XAMPP/xamppfiles/htdocs/project.php on line 17
Run Code Online (Sandbox Code Playgroud)
页面代码:
<html>
<head>
<title>PHP Site Michael Mazur</title>
</head>
<body>
<?php
//connect to DB
$con=mysql_connect("localhost","mike","mike");
$db_found = mysql_select_db("my_guitar_shop2");
$result = mysql_query("SELECT firstName,lastName FROM customers");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['firstName'] . "</td>";
echo "<td>" . $row['lastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
你的while循环的其余部分可能看起来像这样
while($row = mysql_fetch_array($result)){
print "<tr><td>".$row['Firstname']."</td><td>".$row['Lastname']."</td></tr>";
}
print "</table>";
Run Code Online (Sandbox Code Playgroud)
尝试将这些行放在一行上,就像我上面所做的那样.
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
Run Code Online (Sandbox Code Playgroud)
喜欢
echo "<table border='1'><tr><th>Firstname</th><th>Lastname</th></tr>";
Run Code Online (Sandbox Code Playgroud)
其他有用的选项.
http://php.net/manual/en/function.mysql-fetch-array.php
http://php.net/manual/en/control-structures.foreach.php