jwhite1979
New member
I'm new to PHP and MySQL, and something is confusing me.
Why is it that the following code works...
$subject_set = mysql_query("SELECT * FROM subjects", $connection);
if (!$subject_set) {die("This did not work. " . mysql_error());}
while ($subject = mysql_fetch_array($subject_set)) {
echo "<li> {$subject["menu_name"]} </li>";
$page_set = mysql_query("SELECT * FROM pages WHERE subject_id = {$subject["id"]}", $connection);
if (!$page_set) {die("This did not work. " . mysql_error());}
echo "<ul class='pages'>";
while ($page = mysql_fetch_array($page_set)) {
echo "<li> {$page["menu_name"]} </li>";
}
echo "</ul>";
}
But this code returns a mysql error...
$subject_set = mysql_query("SELECT * FROM subjects", $connection);
if (!$subject_set) {die("This did not work. " . mysql_error());}
while ($subject = mysql_fetch_array($subject_set)) {
echo "<li> {$subject["menu_name"]} </li>";
}
$page_set = mysql_query("SELECT * FROM pages WHERE subject_id = {$subject["id"]}", $connection);
if (!$page_set) {die("This did not work. " . mysql_error());}
echo "<ul class='pages'>";
while ($page = mysql_fetch_array($page_set)) {
echo "<li> {$page["menu_name"]} </li>";
}
echo "</ul>";
The difference, of course, is that in the latter instance I am performing the second operation after completing the first operation. In the first example they are both part of the initial "while" loop. In the second case the query fails because it doesn't recognize $subject['id']. Why not? Shouldn't that array be available anywhere on the page? What am I missing?
Thanks!
Why is it that the following code works...
$subject_set = mysql_query("SELECT * FROM subjects", $connection);
if (!$subject_set) {die("This did not work. " . mysql_error());}
while ($subject = mysql_fetch_array($subject_set)) {
echo "<li> {$subject["menu_name"]} </li>";
$page_set = mysql_query("SELECT * FROM pages WHERE subject_id = {$subject["id"]}", $connection);
if (!$page_set) {die("This did not work. " . mysql_error());}
echo "<ul class='pages'>";
while ($page = mysql_fetch_array($page_set)) {
echo "<li> {$page["menu_name"]} </li>";
}
echo "</ul>";
}
But this code returns a mysql error...
$subject_set = mysql_query("SELECT * FROM subjects", $connection);
if (!$subject_set) {die("This did not work. " . mysql_error());}
while ($subject = mysql_fetch_array($subject_set)) {
echo "<li> {$subject["menu_name"]} </li>";
}
$page_set = mysql_query("SELECT * FROM pages WHERE subject_id = {$subject["id"]}", $connection);
if (!$page_set) {die("This did not work. " . mysql_error());}
echo "<ul class='pages'>";
while ($page = mysql_fetch_array($page_set)) {
echo "<li> {$page["menu_name"]} </li>";
}
echo "</ul>";
The difference, of course, is that in the latter instance I am performing the second operation after completing the first operation. In the first example they are both part of the initial "while" loop. In the second case the query fails because it doesn't recognize $subject['id']. Why not? Shouldn't that array be available anywhere on the page? What am I missing?
Thanks!