似乎无法从PHP和MySQL中转义字符

时间:2018-09-05 10:19:24

标签: php mysql

在我的PHP-Codeigniter function()中,我给出了以下命令:

function get_cust($search='David')
{
$query = 'select pp.first_name from Cust pp where'; 
$query .= ' pp.first_name LIKE "%'.$search.'%")';
print_r(mysql_real_escape_string($query));
}

但是当我在浏览器中执行输出时:

select pp.first_name from Cust pp where pp.first_name LIKE 'Úvid%'

反正还是这样,我可以得到它:

select pp.first_name from Cust pp where pp.first_name LIKE '%David%'

我也尝试过使用addslashes()mysql_real_escape_string()

1 个答案:

答案 0 :(得分:-1)

  1. 远离mysql_ *扩展。它们已从PHP 7中完全删除。切换到mysqli扩展名或PDOBetter use prepared statements, to avoid SQL injection issues
  2. 您应该转义参数,而不是整个查询。
  3. You should use single quotes around the parameter,即使使用mysqli_real_escape_string,也可以防止SQL注入。
  4. Set your character set to UTF-8

使用mysqli_real_escape_string在更改的功能下查找:

function get_cust($search='David')
{
    $query = "select pp.first_name from Cust pp where"; 
    $query .= " pp.first_name LIKE '%".mysqli_real_escape_string($search)."%')";
    print_r($query);
}