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 MySQL创建时出现1064以下情况怎么办?
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重