Category/Sub-Category

I am trying to make a list of categories with sub-categories. When I run the script I get
Database query failed:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘’ at line 1

so I added in
$rs = mysql_query($page_set) or die(“Problem with the query: $q at line " . LINE . ‘
’ . mysql_error());”
to debug the issue and it comes back with

“Problem with the query: at line 39 Query was empty”

Here is the script

[php]<?php
$subject_set=mysql_query(“select * from subjects”, $connect);
if(!$subject_set) {
die (“Database Query Failed:” . mysql_error());
}

	while($page=mysql_fetch_array($subject_set)) 
		{
			echo "<li> {$page["menu_name"]}</li>";
	}

	$page_set= mysql_query ("SELECT * FROM pages where subject_id = " .$row['id'] , $connect);
		
		$rs = mysql_query($page_set) or die("Problem with the query: $q at line " . __LINE__ . '<br>' . mysql_error());
		if (!$page_set) {
			die("Database query failed: " . mysql_error());
	
		}
		
		?>[/php]

Hi there,

I see that you are using an array called $row in your second query yet I don’t see any mention of it prior to its use. What i think you are trying to do is perform the second query with the information you are using from the first. You need to enclose everything after the while statement in the while statement and change $row[‘id’] to $page[‘id’]. That is, unless you are using $row[‘id’] somewhere else that is not in your code.

Hope this helps.

I believe I understand what you’re saying… Tho I still can’t quite get it to work… I have changes the code a little bit to make it easier for me to read.

[php]<?php

echo "<ul>";

$category_set = mysql_query( "Select * FROM subjects", $connect);
	if(!$category_set) 
		{
			die( "Data Base Query Failed: " . mysql_error());
		}
		
	while($subject=mysql_fetch_array($category_set))
		{
			echo "<li> {$subject["menu_name"]} </li>" ;
		}

$page_set = mysql_query("SELECT * FROM pages WHERE subject_id = {$subject["id"]}", $connect);
	if(!$page_set) 
		{
			die("Database Query Failed: " . mysql_error());
		}
		
	while($page=mysql_fetch_array($page_set))
		{
			
			echo "<li>{$page["menu_name"]} </li>";
		}
		
		echo "</ul>";

?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service