-4

I am trying to add a second where clause/condition to a query and in doing so I am getting this error: Parse error: syntax error, unexpected ‘=’

This was working fine when the last line of the below code simply read: WHERE stories.SID = $the_SID");

But I need to add more conditions. Probably will need about 3 where clauses once I can figure out how to do 2 properly.

Here is the current code that is problematic on the last line:

$results = $wpdb->get_results("
SELECT wp_users.ID, wp_users.display_name,
stories.SID, stories.story_name, stories.category,
writing.text, writing.approved
FROM stories
INNER JOIN wp_users ON stories.ID = wp_users.ID
INNER JOIN writing ON stories.SID = writing.SID
WHERE (stories.SID = $the_SID") AND (writing.approved = "Y"));

Also....this query will return one record I think. There will be many records with the same SID however, I wan to display all of them that are approved. The approved variable is the only unique identifier. This should work with the above approach correct?

user5000
  • 129
  • 1
  • 9

2 Answers2

1

Your quotation mark is in the wrong place.

$results = $wpdb->get_results("
SELECT wp_users.ID, wp_users.display_name,
stories.SID, stories.story_name, stories.category,
writing.text, writing.approved
FROM stories
INNER JOIN wp_users ON stories.ID = wp_users.ID
INNER JOIN writing ON stories.SID = writing.SID
WHERE (stories.SID = $the_SID) AND (writing.approved = 'Y')");
NDB
  • 63
  • 6
  • My last line now reads as WHERE (stories.SID = $the_SID") AND (writing.approved = 'Y')"); but still the same error... – user5000 Jul 30 '14 at 20:39
  • Remove the quotation mark after '$the_SID' and you'll be golden! – NDB Jul 30 '14 at 20:40
  • That's it! Such a silly error...can't believe I never saw taht. Thanks NDB! – user5000 Jul 30 '14 at 20:43
  • No problem! I'd recommend reading [this stackoverflow answer on debugging syntax errors](http://stackoverflow.com/a/18050072/1645657) to help in debugging syntax errors like this in the future. – NDB Jul 30 '14 at 20:52
0

The first " in "Y" is actually the closing quote to the entire query string. Replace with this

$results = $wpdb->get_results("
SELECT wp_users.ID, wp_users.display_name,
stories.SID, stories.story_name, stories.category,
writing.text, writing.approved
FROM stories
INNER JOIN wp_users ON stories.ID = wp_users.ID
INNER JOIN writing ON stories.SID = writing.SID
WHERE (stories.SID = '$the_SID') AND (writing.approved = 'Y')");
Alex
  • 1,565
  • 2
  • 9
  • 13