简体   繁体   English

在PHP中显示SQL表

[英]Display SQL Table in PHP

I am trying to display a table in php. 我正在尝试在php中显示表格。 I have established a valid connection. 我已经建立了有效的连接。 I get the error: 我收到错误:

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>

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/zh/function.mysql-fetch-array.php

http://php.net/manual/en/control-structures.foreach.php http://php.net/manual/en/control-structures.foreach.php

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调用(可读性的良好实践)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM