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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题