I want to sort the data from a mysql database when clinking in the header of a html table. I wrote this code:
$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>";
But on the screen does not appear any data whatsoever 🙁 What I did wrong?
You’re executing your query at
then attempting to appen to that query object at
You need to build the query string first
so
append your
ORDER BYthen execute
EDIT: Also remove
$new_word2 = mysql_query($new_word);and change your while to use$new_worldas Uttam Kadam says in their answer