hello guys, just want to ask some newbie questions. i have a table named "students" in my database, the field names are "Lastname", "Firstname" and "Highschool". i want to sort my query by highschool field. i want to show that query results in table form. how can i sort by showing all schools in the database and under each schools, i want all students' names that are in the same school.

i use these codes:

<?php

        $query = "SELECT * FROM students ORDER by highschool";

        $result = mysql_query($query);
        $numrows = mysql_num_rows($result);

        
        while($row = mysql_fetch_assoc($result)) {
        echo "<table border='1'>";
        echo "<tr>";
        echo "<th align='left' width='450'>" . $row['highschool'] . "</th>";
        echo "</tr>";
        echo "<tr>";
        echo "<td width='450'>" . $row['lastname'] . ", " . $row['firstname'] . "</td>";
        echo "</tr>";
        echo "<p>&nbsp;</p>";
        }

?>

i have some image attachment to explain more.

Maybe you should try something like this;

$query = "SELECT DISTINCT highschool FROM students ORDER BY highschool";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
echo "<table border='1'>";
	echo "<tr>";
	echo "<th align='left' width='450'>" . $row['highschool'] . "</th>";
	echo "</tr>";
		$query2 = "SELECT * FROM students WHERE highschool = $row[highschool] ORDER BY lastname";
		$result2 = mysql_query($query2);
		while($row2 = mysql_fetch_assoc($result2))
			{
				echo "<tr>";
				echo "<td width='450'>" . $row2['lastname'] . ", " . $row2['firstname'] . "</td>";
				echo "</tr>";
			}
echo "</table>";
echo "<br />";
}

but i am getting errors when i used that code.

it says: "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/marvin26/public_html/thesis/view1.php on line 40"

edited:
in this line:

"$query2 = "SELECT * FROM students WHERE highschool = $row[highschool] ORDER BY lastname";"

i just placed a single qoute in the $row[highschool].

tnx!

try this:

$query2 = "SELECT * FROM students WHERE highschool ='".$row['highschool']."' ORDER BY lastname";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.