在 PHP 中显示 SQL 表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8437685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display SQL Table in PHP
提问by YoungGuy
I am trying to display a table in php. I have established a valid connection. I get the error:
我正在尝试在 php 中显示一个表格。我已经建立了有效的连接。我收到错误:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /Applications/XAMPP/xamppfiles/htdocs/project.php on line 17
The page's code:
页面代码:
<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>
回答by jamesTheProgrammer
The rest of your while loop could look like this
while 循环的其余部分可能如下所示
while($row = mysql_fetch_array($result)){
print "<tr><td>".$row['Firstname']."</td><td>".$row['Lastname']."</td></tr>";
}
print "</table>";
Try putting these lines on a single line as i have done above.
像我上面所做的那样,尝试将这些行放在一行上。
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
like
喜欢
echo "<table border='1'><tr><th>Firstname</th><th>Lastname</th></tr>";
Other useful options here.
其他有用的选项在这里。
http://php.net/manual/en/function.mysql-fetch-array.php
http://php.net/manual/en/function.mysql-fetch-array.php
回答by warren
Given that part of the page's code is missing, this is only a guess. But it looks like part of your problem is that you've double-selected the database (a no-no).
鉴于页面代码的那部分缺失,这只是一个猜测。但看起来你的问题的一部分是你已经双重选择了数据库(一个禁忌)。
Also, the while
statement looks slightly suspect (no opening brace to verify context).
此外,该while
语句看起来有点可疑(没有左大括号来验证上下文)。
Additionally, if you are going to pass $con
to the database selector, you should also pass it to the mysql_query
calls (good practice for readability).
此外,如果您要传递$con
给数据库选择器,您还应该将其传递给mysql_query
调用(提高可读性的良好做法)。