我正试图在CodeIgniter中运行全文索引搜索。
我的搜索功能如下:
public function get_searchresult_count($searchresult)
{
$today_date = date('Y-m-d');
$db_photos = $this->load->database('photos', TRUE);
$db_photos->select('*');
$db_photos->from('photos');
$db_photos->where('MATCH (`title`, `description`) AGAINST ("'.$searchresult.'")', NULL);
$db_photos->where('approved', '1');
return $db_photos->count_all_results();
}
我需要返回为count_all_results
,因为它需要获得一个用于对结果进行分页的数字。
我不能让它工作,我得到这个错误:
错误编号:1064
您的SQL语法有错误;查看手册与您的MySQL服务器版本相对应,以便使用正确的语法接近第3行的"反对"("石灰")和
approved
='1'从(
photos
)中选择计数(*)作为numrows
,其中MATCH
(title
,description
)对抗("石灰")和approved
='1'
有什么想法吗?
您需要将where()
函数的第三个参数设置为false,以防止参数自动转义。否则,match子句中的引号将被转义,并创建无效查询。
逐字取自AR手册页面
$this->db->where()接受可选的第三个参数。如果您将其设置为>FALSE,CodeIgniter将不会尝试使用反标记来保护字段或表名。
$this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE);
http://codeigniter.com/user_guide/database/active_record.html
您需要运行选择查询并返回结果。在"where"子句之后,尝试添加:
$query = $db_photos->get();
return $query->count_all_results();
希望能有所帮助。