简体   繁体   中英

Display SQL Table in PHP

I am trying to display a table in 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($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/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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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