我正在使用分页搜索结果,但出于某种原因,我的搜索脚本无效。我似乎找不到将LIMIT $start, $display
放在我的搜索查询代码中的正确位置,我把它放在我的代码中,它显示分页错误。有人可以帮助我吗?
这是我的PHP&部分。 MySQL搜索代码。
$mysqli = mysqli_connect("localhost", "root", "", "sitename");
mysqli_select_db($mysqli, "sitename");
$search_explode = explode(" ", $search);
foreach($search_explode as $search_each) {
$x++;
if($x == 1){
$construct .= "article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
} else {
$construct .= "OR article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
}
}
$construct = "SELECT users.*, users_articles.* FROM users_articles
INNER JOIN users ON users_articles.user_id = users.user_id
WHERE $construct";
$run = mysqli_query($mysqli, $construct);
$foundnum = mysqli_num_rows($run);
SQL语句。
SELECT users.*, users_articles.* FROM users_articles INNER JOIN users ON users_articles.user_id = users.user_id WHERE article_content LIKE '%find%' OR title LIKE '%find%' OR summary LIKE '%find%'OR article_content LIKE '%this%' OR title LIKE '%this%' OR summary LIKE '%this%'OR article_content LIKE '%article%' OR title LIKE '%article%' OR summary LIKE '%article%' LIMIT 0, 10
答案 0 :(得分:2)
SELECT的基本语法是:
SELECT [fields]
FROM [tables [JOIN tables]]
WHERE [conditions]
GROUP BY [fields]
HAVING [conditions]
ORDER BY [fields]
LIMIT [limit]
所以,在WHERE条件之后的最后。
以下是显示full syntax。
的文档答案 1 :(得分:1)
您是否还记得在构建查询时在字符串片段中插入空格?请记住,PHP不会将任何内容放入您自己没有放入的字符串中。
$x = 'a';
$x .= 'b';
为您提供ab
,而不是a[space]b
。如果输入了多个搜索字词,则$construct
将会出现语法错误,因为后续OR
将直接附加到之前添加的搜索字词的末尾。你最终会得到类似的东西:
[...snip...] OR summary LIKE '%$search_each%OR article_content LIKE[...snip...]
^^--syntax error here.
如果您附加限制子句而不确定查询结尾与您追加的“LIMIT X,Y”文本之间是否有空格,则同样适用。
答案 2 :(得分:0)
这应显示前10行:
SELECT users.*, users_articles.* FROM users_articles
INNER JOIN users ON users_articles.user_id = users.user_id
WHERE article_content LIKE '%something%'
LIMIT 0, 10
将LIMIT
子句更改为以下内容应显示11到20之间的行:
LIMIT 10, 10
另请注意,您可能应该将$construct
字符串转义为上面评论中建议的@Col. Shrapnel。这是为了避免SQL injection。
此外,您可能希望使用Full Text Indexing进行调查。
答案 3 :(得分:0)
$mysqli = mysqli_connect("localhost", "root", "", "sitename");
mysqli_select_db($mysqli, "sitename");
$search_explode = explode(" ", $search);
foreach($search_explode as $search_each) {
$x++;
if($x == 1){
$construct .= " article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
} else {
$construct .= " OR article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
}
}
$construct = "SELECT users.*, users_articles.* FROM users_articles
INNER JOIN users ON users_articles.user_id = users.user_id
WHERE $construct";
$construct .= " LIMIT 0, 10" ; // replace 0, 10 with your variables
$run = mysqli_query($mysqli, $construct);
$foundnum = mysqli_num_rows($run);