简体   繁体   English

Postgres-如何从表中获取转义数据

[英]Postgres - How to fetch escaped data from table

The solution seems to be simple but I am finding it hard to get around. 解决方案似乎很简单,但我发现很难解决。 In the table, the sample data is as: 在表中,样本数据如下:

 id   |               name               |        created_time        
 -------+----------------------------------+----------------------------
41477 | women''s hostels                 | 2016-03-24 13:52:52.855116
41476 | women''s hostels                 | 2016-03-24 13:52:49.895482
41475 | women''s hostels                 | 2016-03-24 13:52:49.735788
41471 | men''s hostel                    | 2016-03-24 13:52:42.907983
41469 | women''s clothing shops          | 2016-03-24 13:52:30.666364

I have inserted this data using PHP's PDO prepared statements so the single quotes have been escaped properly. 我已使用PHP的PDO预准备语句插入了此数据,因此单引号已正确转义。

Now I want to fetch the records where name exactly matches 现在我想获取名称完全匹配的记录

women's hostels 妇女旅馆

I want to accomplish this in both ways using raw SQL query and through PDO. 我想使用原始SQL查询和PDO通过两种方式完成此操作。 I am trying following for both cases, but not getting any result: 在这两种情况下,我都尝试执行以下操作,但未得到任何结果:

Raw SQL: 原始SQL:

SELECT id FROM <TABLE> where name = 'women''s hostels';
SELECT id FROM <TABLE> where name ilike 'women''s hostels';
SELECT id FROM <TABLE> where name::text = 'women''s hostels';

PDO: PDO:

$data = "women''s hostels";
$sql = "SELECT id FROM <TABLE> where name=?"; (tried all variations as above)
$stmt->execute(array($data));
$rows = $stmt->fetchAll();

The name field is defined in table as below: 名称字段在表中定义如下:

Column    |            Type             | 
name      | character varying(255)      |  

Please help. 请帮忙。 Let me know if anything else is needed. 让我知道是否还有其他需要。 Any help will be appreciated. 任何帮助将不胜感激。

There should be no escaped data in database in the first place. 首先,数据库中不应有转义数据。

I have inserted this data using PHP's PDO prepared statements so the single quotes have been escaped properly. 我已使用PHP的PDO预准备语句插入了此数据,因此单引号已正确转义。

They have been not. 他们没有。

Were they escaped properly, you'd have the original string in database, "women's hostels". 如果他们正确地逃脱了,您将在数据库“ women's hostels”中找到原始字符串。 But as you have your data in database escaped, it means that escaping occurred twice . 但是,当您对数据库中的数据进行转义时,这意味着转义发生了两次

So you have to hunt down and remove that superfluous escaping during insert . 因此,您必须搜寻并删除插入过程中多余的转义。

Then select whatever data usual way. 然后选择通常的数据方式。 It makes no difference whether you have quotes in it or not. 是否包含引号都没有关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM