PHP/MySQL - 搜索查询 [英] PHP / MySQL - Search query
本文介绍了PHP/MySQL - 搜索查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要帮助对评论进行搜索查询(针对 WordPress 网站).
I need help making a search query for comments (it's for a WordPress site).
评论以这种方式检索 - 如果用户已登录:
the comments are retrieved this way- if user is logged in:
$comments = $wpdb->get_results($wpdb->prepare("
SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d
AND (comment_approved = '1' OR (user_id = %d AND comment_approved = '0'))
ORDER BY comment_date_gmt", $post->ID, $user_ID));
如果不是:
$comments = $wpdb->get_results($wpdb->prepare("
SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d
AND (comment_approved = '1' OR (comment_author = %s
AND comment_author_email = %s AND comment_approved = '0'))
ORDER BY comment_date_gmt",
$post->ID, wp_specialchars_decode($comment_author,ENT_QUOTES),
$comment_author_email));
那么我如何过滤包含特定搜索字符串的评论,例如 $_GET['search_query']
?
so I how can I filter comments that contain a specific search string, like $_GET['search_query']
?
这是 WP 的数据库结构.我要查找的搜索字符串在 comment_content
this is WP's database structure. The search string I'm looking for is in comment_content
推荐答案
use LIKE
$comments = $wpdb->get_results($wpdb->prepare("
SELECT * FROM $wpdb->comments WHERE comment_content LIKE ('%$_GET['search_query']%')
and comment_post_ID = %d
AND (comment_approved = '1' OR (user_id = %d AND comment_approved = '0'))
ORDER BY comment_date_gmt", $post->ID, $user_ID));
这篇关于PHP/MySQL - 搜索查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文