0

我想SELECT从表中获取一些行,但这些行必须满足类似的条件column = value。这是我的SELECT声明:

$query = "SELECT t1.title, t1.introtext, t2.jr_street, t2.jr_city, 
    t2.jr_state, t2.jr_postalcode, t2.jr_country, t3.created, t3.name, 
    t3.title, t3.comments, t4.ave 
FROM $table1 t1
    LEFT OUTER JOIN $table2 t2 ON t1.id = t2.contentid
    LEFT OUTER JOIN $table3 t3 ON t1.id = t3.pid
    LEFT OUTER JOIN $table4 t4 ON t1.id = t4.reviewid
";

我尝试在语句WHERE后添加子句FROM,但出现 SQL 语法错误。

4

1 回答 1

1

看来我得猜到你的问题了。下次你应该给我们你的完整例子一个确切的错误。你尝试过这样的事情吗?

$value = (int) $_POST['value'];
$query = "SELECT t1.title, t1.introtext, t2.jr_street, t2.jr_city, t2.jr_state,
t2.jr_postalcode, t2.jr_country, t3.created, t3.name, t3.title, 
t3.comments, t4.ave FROM $table1 t1
LEFT OUTER JOIN $table2 t2 ON t1.id = t2.contentid
LEFT OUTER JOIN $table3 t3 ON t1.id = t3.pid
LEFT OUTER JOIN $table4 t4 ON t1.id = t4.reviewid
WHERE t2.column = ".$value."
AND t1.catid=8";

我只是猜你想从 table2 中选择;)你必须填写正确的表格和列。我还将类别选择放在 where 子句中,因为恕我直言,这使它更具可读性。

请绝对确保所有变量$value(和表)都保存以供查询中使用,例如,它$value是一个整数,并且表是在您的代码中明确设置的,而不是用户输入。您可以在此处阅读有关 SQL 注入的更多信息:

于 2013-05-27T10:06:25.180 回答