您如何编码单引号,以便在mysql中可搜索到? [英] How do you encode an apostrophe so that it's searchable in mysql?

查看:90
本文介绍了您如何编码单引号,以便在mysql中可搜索到?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这不是最明确的问题,但是通过示例可以更清楚一些.

I don't think that was the most clear question, but an example should make it a little clearer.

我有一张桌子,上面放着电影名称,其中一些包含撇号.我有一个用于查找电影的搜索框.

I have a table filled with movie names, some of which contain apostrophes. I have a search box which is used to find movies.

如果我通过

mov_title = '$search_keywords'

所有方法都可以,但是对于部分搜索,此方法不会产生任何结果,因此我必须使用此方法

it all works, but this method will not yield any results for partial searches, so I have to use this

mov_title LIKE '%$search_keywords%'

此方法对于A-Za-z0-9的标题来说效果很好,但是即使标题带有撇号,即使我进行了完全匹配,也无法找到电影.

This method works fine for titles that are A-Za-z0-9, but if a title has an apostrophe, it's not able to find the movie, even if I do an exact match.

在将标题存储在数据库中之前,我通过以下步骤进行了放置:

Before the titles are stored in the DB, I put them through this:

$search_keywords = htmlspecialchars(mysql_escape_string($_GET["search_keywords"]));

因此,在数据库中,每个单撇号前都有一个正斜杠.

So in the DB, there is a forward slash before every single apostrophe.

将影片标题与撇号匹配的唯一方法是在搜索框中以物理方式在撇号前面放置一个正斜杠.

The only way to match a movie title with an apostrophe is to physically put a forward slash in front of the apostrophe, in the search box.

这似乎很琐碎,我确定解决方案非常痛苦,但我只是没有看到它.

This seems so trivial, and I'm sure the solution is painfully obvious, but I'm just not seeing it.

推荐答案

使用mysql_real_escape_string(),而不使用htmlspecialchars().后者不是用于数据库转义,而是用于HTML生产.

Use mysql_real_escape_string(), and do not use htmlspecialchars(). The latter is not for database escaping, it's for HTML production.

这篇关于您如何编码单引号,以便在mysql中可搜索到?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆