我正在使用两个表进行选择,但我没有返回行。
但如果我只用一张桌子(新闻或页面)进行选择,那就完美了。
这是我的代码:
$pdo = conecting();
$read = $pdo->prepare("SELECT title,content FROM news, pages WHERE (title LIKE ? OR content LIKE ?)");
$read->bindValue(1,"%$search%", PDO::PARAM_STR);
$read->bindValue(2,"%$search%", PDO::PARAM_STR);
$read->execute();
echo '<p>Your search returned <strong>'.$read->rowCOunt().' results!</strong></p>';
if($read->rowCount() <=0){
echo '<h2>We didnt found any result for your search.</h2>';
}
你认为这个问题有什么原因吗?
如果我只使用一个表,我总是返回行,如下所示:
$read = $pdo->prepare("SELECT title, content FROM pages WHERE (title LIKE ? OR content LIKE ?)")
;
或只是新闻:
$read = $pdo->prepare("SELECT title, content FROM news WHERE (title LIKE ? OR content LIKE ?)")
答案 0 :(得分:1)
,
就像使用联接一样,所以你基本上在做SELECT cols FROM news JOIN pages
。如果两个表都有title
和content
列,则会导致错误。在开发过程中,您应该打开error reporting。
在这种情况下,您可能想要使用联合。
SELECT title, content FROM news WHERE ($clause)
UNION title, content FROM pages WHERE ($clause)
请记住,这需要绑定四个值。