doumanni3501 2015-09-24 08:22
浏览 45
已采纳

字符串包含php mysql中的特殊字符

I have data in mysql table something like this (I'm working on kruti dav 010 font)

id   name      description 
1     'abc       '"dbs
2     a'bc       <'dns
3     /bc        sb 'dcv
4     x"ydbv     er?$#nv
5     y>/'b      'Dfr>?
6     p;q'd      'df@'d

Now i want to perform some operations like

INSERT INTO table_name (name,description ) VALUES ('x"ydv',''"dbs')

When i run this query values inside of single quotes there is an error because of '"dbs

INSERT INTO table_name (name,description ) VALUES ("x"ydv","'"dbs")

Similarly when i put values inside of double quotes there is an error because of x"ydv and '"dbs Now when i want to perform some operation like

<?php
$string='dbx;'pq';
echo $string;
?>

There is an error because of single quote inside of string I know solution of this problem using \ i can escape single quote inside of string.

Last problem

<?php
$array=Array ( [0] => dchj/kke [1] => jktukanxkao [2] => dksfj;k [3] => ukjk;.kiqj [4] => t'kiqj [5] => jk;x<+ [6] => "dksjck );
print_r($array);
$fields="'".implode("','", $array)."'";
    echo $fields;
$this->db->select('id')->from('village')->where_in("name", $fields);
$query = $this->db->get();
echo $this->db->last_query();
?>

Output

Array ( 
[0] => dchj/kke 
[1] => jktukanxkao 
[2] => dksfj;k 
[3] => ukjk;.kiqj 
[4] => t'kiqj 
[5] => jk;x<+ 
[6] => "dksjck )

'dchj/kke','jktukanxkao','dksfj;k','ukjk;.kiqj','t'kiqj','jk;x<+','"dksjck'

SELECT `id` FROM (`village`) WHERE `name` IN('\'dchj/kke\',\'jktukanxkao\',\'dksfj;k\',\'ukjk;.kiqj\',\'t\'kiqj\',\'jk;x<+\',\'"dksjck\'')

New when i run this query in mysql

 MySQL returned an empty result set (i.e. zero rows). (Query took 0.0025 sec)

i have records in database for given array, but mysql returns zero rows

  • 写回答

1条回答 默认 最新

  • dss89001 2015-11-06 17:38
    关注

    The easiest solution to this is to use the native CodeIgniter query builder: Where ever your are building your insert statements in your application try the following, of course replacing the values of name and description with the appropriate information.:

    $this->db->insert('tablename',array('name' => 'x"ydv', 'description' => '"\'"dbs"' ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题