添加第二个where子句时出现语法错误

时间:2014-07-30 20:35:07

标签: php mysql

我正在尝试向查询添加第二个where子句/条件,并且这样做我收到此错误:解析错误:语法错误,意外'='

当下面代码的最后一行只是读取时,这工作正常:     在哪里story.SID = $ the_SID“);

但我需要添加更多条件。可能需要大约3个where子句,一旦我能弄清楚如何做好2。

以下是最后一行有问题的当前代码:

$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"));

此外....此查询将返回我认为的一条记录。将会有许多具有相同SID的记录,但我想显示所有已批准的记录。已批准的变量是唯一的唯一标识符。这应该与上述方法一致吗?

2 个答案:

答案 0 :(得分:1)

您的引号位置错误。

$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')");

答案 1 :(得分:0)

"中的第一个"Y"实际上是整个查询字符串的结束引用。替换为

$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')");