I'm trying to do nested mysql query but I'm failing it's as simple as I have a table called 'todo' which is for saving to-do list when I do well this code is just to show how I wanna do it logically , I know it doesn't work and I think it probably needs JOIN or UNION but I couldn't do it
$result = mysqli_query($con,"SELECT type FROM todo WHERE user = '$username' GROUP BY type");
while($row = mysqli_fetch_array($result))
{
echo '<td>- <a href="list.php?type='.$row['type'].'">'." ".$row['type'].'</a></td>';
echo "</br>";
$result2 = mysqli_query($con,"SELECT titleFROM todo WHERE type= '$row['type']'");
while($row = mysqli_fetch_array($result2))
{
echo '<td>-- <a href="todo.php?type='.$row['title'].'">'." ".$row['title'].'</a>
}
</td>';
echo "</br>";
}
I want the result to like -Work --Mr Jack --Company -School --Android Class --Entrepreneurship --php development
and so on...
Your inner loop overwrites the outerloop's $row
variable. Try using a different variable name in the inner loop. Also, there's an error in your inner loop's SQL query (missing space between the tablename and FROM
keyword).