Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php-PDO准备带破折号的bindParam不工作_Php_Mysql_Pdo - Fatal编程技术网

php-PDO准备带破折号的bindParam不工作

php-PDO准备带破折号的bindParam不工作,php,mysql,pdo,Php,Mysql,Pdo,我的数据库中有法国城市,如下所示: 01001 Abergement-Clémenciat 01002 Abergement-de-Varey 01004 Ambérieu-en-Bugey 01005 Ambérieux-en-Dombes 01006 Ambléon 01007 Ambronay 01008 Ambutrix 01009 Andert-et-Condon 01010 Anglefort 我有一个PDO准备查询: $sth = $this-

我的数据库中有法国城市,如下所示:

01001   Abergement-Clémenciat
01002   Abergement-de-Varey
01004   Ambérieu-en-Bugey
01005   Ambérieux-en-Dombes
01006   Ambléon
01007   Ambronay
01008   Ambutrix
01009   Andert-et-Condon
01010   Anglefort
我有一个PDO准备查询:

$sth = $this->connection->prepare('
        SELECT code
        FROM insee
        WHERE city = :city
    ');
$sth->bindParam(':city', $city, PDO::PARAM_STR);
$sth->execute();

$row = $sth->fetch();
当city等于“Ambronay”或“Ambléon”或“Ambutrix”时,fetch将返回一些东西。 但当city等于“Abergement de Varey”或“Saint-Étienne”或其他带有破折号(-)的值时,fetch在数据库中时不返回任何内容

原因可能是什么


谢谢您的支持。

这似乎是一个编码问题。此时,您应该强制php对
$city
使用多字节字符串。如果问题仍然存在,请检查您的数据库编码,这应该是utf8这样的stings。如果问题现在仍然存在,请检查驱动程序连接编码。请注意,此设置非常依赖于数据库。例如。在MySQL中,您可以为每个表定义不同的字符集,而这在其他数据库中是不可能的。

谢谢@alpham8

我在调用PDO时忘记了输入编码,它现在可以工作了

因此,解决方案是:

$this->connection = new PDO($dns, $user, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));

尝试转义破折号:
$city=preg\u replace(“/\-/”、“\-”、$city)谢谢,但结果是一样的。提取没有返回任何内容。