drctyr2869 2012-08-04 21:54
浏览 32
已采纳

如何在php中转义字符串? [重复]

Possible Duplicate:
How to properly escape a string via PHP and mysql

I'm trying to populate a MySQL database, and to do so I'm parsing a data file and running a INSERT INTO ... query. the table parsonspredictions_R is structured as:

+-------------+---------+------+-----+---------+----------------+
| Field       | Type    | Null | Key | Default | Extra          |
+-------------+---------+------+-----+---------+----------------+
| id          | int(11) | NO   | PRI | NULL    | auto_increment |
| drug_a      | blob    | NO   |     | NULL    |                |
| drug_b      | blob    | NO   |     | NULL    |                |
| correlation | float   | NO   |     | NULL    |                |
| p_value     | float   | NO   |     | NULL    |                |
+-------------+---------+------+-----+---------+----------------+

However, there are some drug_x values which have ' along, so how could I escape my string in other to ignore or remove those ' from the string?

Example:

INSERT INTO parsonspredictions_R (
    drug_a,
    drug_b,
    correlation,
    p_value
) VALUES(
   '2'-Hydroxyflavanone_28_0',
   'Emodin',
   0.165714,
   0.0019
);

Results in:

Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '', 'Emodin', 0.165714, 0.0019 )' at line 1-b

  • 写回答

2条回答 默认 最新

  • dongshendie8849 2012-08-04 22:00
    关注

    You can escape '-character by typing

    INSERT INTO parsonspredictions_R (
        drug_a,
        drug_b,
        correlation,
        p_value
    ) VALUES(
        '2''-Hydroxyflavanone_28_0',
        'Emodin',
        0.165714,
        0.0019
    );
    

    Or you can put your string in MySQL query in ""-quotes.

    edited: To make your file ready for regex you can use mysql_real_escape_string() on text before using regex. It will escape all suspicious characters, like '-character.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊