0

我想在 html 表的标题中对来自 mysql 数据库的数据进行排序。我写了这段代码:

$username = $_SESSION['username']; // gets the username

echo "<table class='normal_table'>";

$new_word = mysql_query("SELECT * FROM new_word WHERE username='$username'");
$new_word2 = mysql_query($new_word);

echo " 

<tr>
<th><a href='test.php?sort=cz'>Czech:</a></th>
<th><a href='test.php?sort=en'>English:</a></th>
<tr>";

if ($_GET['sort'] == 'cz')
{
 $new_word .= " ORDER BY cz";
}
elseif ($_GET['sort'] == 'en')
{
 $new_word .= " ORDER BY en";
}

while($new_word2 = mysql_fetch_array($new_word)){

echo "
<tr>
<td> ".$new_word2['cz']."</td>
<td>".$new_word2['en']."</td>
</tr>

"; }

echo "</table>";

但是屏幕上没有出现任何数据:(我做错了什么?

4

2 回答 2

2

您正在执行您的查询

$new_word = mysql_query("SELECT * FROM new_word WHERE username='$username'");

然后尝试在

$new_word .= " ORDER BY en";

您需要先构建查询字符串

所以

$sql =  "SELECT * FROM new_word WHERE username='$username'"

附加你的ORDER BY

$sql .= " ORDER BY en";

然后执行

$new_word = mysql_query($sql);

编辑:还删除$new_word2 = mysql_query($new_word);并更改您的使用时间,$new_world正如 Uttam Kadam 在他们的回答中所说

于 2012-12-11T10:37:02.100 回答
1
$new_word2 = mysql_query($new_word); //$new_word wrong

删除此行这是额外的。

于 2012-12-11T10:36:30.647 回答