php中的HTML表错误
我是php新手,我尝试过很多次来解决这个问题,但是我做不到。 这是我的php代码 有人能帮我吗这对一个php专家来说应该很容易php中的HTML表错误,php,Php,我是php新手,我尝试过很多次来解决这个问题,但是我做不到。 这是我的php代码 有人能帮我吗这对一个php专家来说应该很容易 <?php require_once 'connector.php'; $result = mysql_query('SELECT * FROM highscores ORDER BY score DESC'); $username = mysql_query('SELECT username FROM users WHERE id in(SELECT user_
<?php
require_once 'connector.php';
$result = mysql_query('SELECT * FROM highscores ORDER BY score DESC');
$username = mysql_query('SELECT username FROM users WHERE id in(SELECT user_id FROM highscores)');
echo"<html>
<head>
<title>Highscores</title>
</head>
<body>
<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>
";
while ($name = mysql_fetch_array($username) )
{
echo "<tr>
<td>" . $name ['username'] . "</td>";
}
while( $row = mysql_fetch_array($result))
{
echo"
<td>" . $row ['score'] . "</td>
<td>" . $row ['date'] . "</td>
</tr>";
}
echo"
</table>
</body>
</html>
";
您可以使用带(.)运算符的字符串连接,而不是将所有内容都放在双引号中。比如说,不是这样写的
echo"<html>
<head>
<title>Highscores</title>
</head>
<body>
<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>
";
echo”
最高得分
用户
分数
日期
";
你可以这样写:
echo "<html>" .
"<head>" .
"<title>Highscores</title>"
"</head>" .
"<body>" .
"<table border='1'>" .
"<tr>" .
"<th>user</th>" .
"<th>score</th>" .
"<th>Date</th>" .
"</tr>" ;
echo”“。
"" .
“高分”
"" .
"" .
"" .
"" .
“用户”。
“得分”。
“日期”。
"" ;
下面的代码块
echo "<tr>
<td>" . $name ['username'] . "</td>";
echo”
" . $名称['username']。"";
应写为
echo "<tr>" .
"<td>" . $name ['username'] . "</td>";
echo”“。
"" . $名称['username']。"";
这样代码看起来也更可读。第二个循环需要在第一个循环内,而结束的
不应该在第二个循环内
<?
while ($name = mysql_fetch_array($username)) {
echo "<tr>";
echo "<td>" . $name["username"] . "</td>";
while ($row = mysql_fetch_array($result)) {
echo "<td>" . $row["score"] . "</td>";
echo "<td>" . $row["date"] . "</td>";
}
echo "</tr>";
}
?>
mysql_*已被弃用!使用mysqli*
<?php
require_once 'connector.php';
$SQL = "SELECT u.username, h.score, h.date
FROM users AS u
JOIN highscores AS h ON (h.user_id = u.users_id)";
$result = mysql_query($SQL) or die( mysql_error() );
echo "<html>
<head>
<title>Highscores</title>
</head>
<body>";
if( mysql_num_rows($result) > 0 )
{
echo "<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>";
while ( $row = mysql_fetch_array($result) )
{
echo "<tr>";
printf("<td>%s</td>", $row['username']);
printf("<td>%s</td>", $row['score']);
printf("<td>%s</td>", $row['date']);
echo "</tr>";
}
echo "</table>
</body>
</html>";
}
mysql_free_result($result);
你能试试这个吗
$result = mysql_query('SELECT hs.score, hs.date, u.username FROM highscores as hs, users as u where hs.user_id=u.id ORDER BY score DESC');
echo "<html>
<head>
<title>Highscores</title>
</head>
<body>
<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>
";
while( $row = mysql_fetch_array($result))
{
echo"<tr>
<td>" . $row ['username'] . "</td>
<td>" . $row ['score'] . "</td>
<td>" . $row ['date'] . "</td>
</tr>";
}
$result=mysql\u query('SELECT hs.score,hs.date,u.username FROM highscores as hs,users as u其中hs.user\u id=u.id ORDER BY score DESC');
回声“
最高得分
用户
分数
日期
";
while($row=mysql\u fetch\u数组($result))
{
回声“
“$row['username']”
“$row['score']”
“$row['date']”
";
}
不要执行两个查询,试着看看你是否可以使用join来处理一个查询。请试着描述你的挑战所在,并展示你已经尝试解决的问题。同时描述一下,你想要达到的结果是什么。对于那些想帮助别人的人来说,总是很难首先通过外部链接找出问题所在;-)