1

I'm a beginner and trying to get a handle on php. I have been getting a syntax error that I can't seem to solve. I'll show you the code below and some of the fixes I've tried. If anyone has another idea that would be wonderful. Thank you:)

$subject_set = mysql_query("SELECT * FROM subjects", $connection);
if(!$subject_set){
  die("Database query failed: " . mysql_error());
}

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

$page_set = mysql_query("SELECT * FROM pages WHERE id_subjects = {$subject["id"]}", $connection);
if(!$page_set){
  die("Database query failed: " . mysql_error());
}

echo "<ul class='pages'>";
while($page = mysql_fetch_array($page_set)) {
  echo "<li> {$page['menu_name']} </li>";
}
echo "</ul>";

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

I know the problem is at {$subject["id"]} because I got content back and no error when I put "WHERE id_subjects = 1". I've tried:

{$subject['id']}
{$subject[\"id\"]}

But have gotten the same error...

1
  • You are using double quotes inside a double quoted string, that's why an error is thrown. Commented May 23, 2012 at 14:44

4 Answers 4

2

try

$page_set = mysql_query("SELECT * FROM pages WHERE id_subjects = '".$subject["id"]."'", $connection);
                if(!$page_set){
                    die("Database query failed: " . mysql_error());
                }

BTW. you should really move away from mysql_* functions. They are being deprecated, move to PDO or mysqli_*, which are a lot safer as well (you are now vulnerable to sql injection)

Sign up to request clarification or add additional context in comments.

Comments

0

If you read back to your post, you can clearly see what's going wrong here.

"SELECT * FROM pages WHERE id_subjects = {$subject["id"]}"

As you can see "id" is not connected to the rest of the rest. That is because with the " you close the string.

To fix this simply use

"SELECT * FROM pages WHERE id_subjects = " . $subject["id"]

Or if you really want to put the variable within the string you can use a single quoted string for the key:

"SELECT * FROM pages WHERE id_subjects = {$subject['id']}"

Personally I am a fan of the first solution. But that is just my opinion.

Comments

0

Well when the while loop finishes looping through, it will have exhausted all the results. $subject['id'] won't have any information simply because $subject no longer has any more entries.

I'm guessing you want to list all the subjects first, then all the pages underneath each subject.

Using mySQL isn't going to be pretty but here's what you want to do. (As Bono said use PDO or mysqli, but here's a solution in psuedocode that will work with mySQL).

loop through first query
    print subject name
    select pages using subject id
    loop through pages under that subject id
       print page names

Comments

0

You don't need any quotes when inside a quoted string, just use

"SELECT * FROM pages WHERE id_subjects = {$subject[id]}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.