Codeigniter自动删除数据库查询中的斜杠?

时间:2011-06-19 17:39:15

标签: php codeigniter

我有一个SQL INSERT查询,其中包含一个包含斜杠的字段。这是PHP中的字符串:

'\\\\192.168.2.10\\datastore'

这是我回应的时候:

'\\192.168.2.10\datastore'

上述字符串是查询的一部分,在此方法中使用:

$this->db->query($sql);

但是,当我查看数据库时,它被写成:

\192.168.2.10datastore

如何阻止这种情况发生?我该怎样关闭以阻止codeigniter这样做?

更新

var $clients_array = array(
    1 => array('datastore' => '\\\\192.168.2.10\\datastore'),
);

function(){

//loop here

$client_details = $this->clients_array[1];
$datastore = $client_details['datastore']; 

$sql = "INSERT INTO table (datastore) VALUES ('$datastore')";

//$sql = "INSERT INTO table (datastore) VALUES ('".$datastore."')"; //tried this too

echo $sql;

if($this->db->query($sql)){}

}

1 个答案:

答案 0 :(得分:3)

我可能会忽略某些东西,但是你知道,在PHP和MySQL中,这些反斜杠都是转义字符。您需要为了查询而逃避它们,否则它们将自行逃脱。

$datastore = $this->db->escape($client_details['datastore']);
$sql = "INSERT INTO table (datastore) VALUES ('$datastore')";

我很确定mysql_real_escape_string()也会很好,但我们正在使用CI,所以不妨充分利用它。

考虑使用CI的Active Record,它会自动转义所有查询,或者查询绑定(链接页面中的引用),否则你必须像往常一样手动执行。

有关使用CI转义查询的更多信息:http://codeigniter.com/user_guide/database/queries.html