如何从数据库中选择包含a,Á,É等特殊字符的数据?

时间:2018-03-21 17:25:14

标签: php mysql sql pdo phpmyadmin

这是表格:

//Creating The Table.
CREATE TABLE `locations` ( 
    `location` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
    `id` INT NOT NULL AUTO_INCREMENT ,
    PRIMARY KEY (`id`)
) ENGINE = InnoDB;

//Inserting 2 Values To The Table , One Of Them Containing Special Char.
INSERT INTO `locations` (`location`, `id`) VALUES ('New York', NULL), ('Alfonso Castaٌeda', NULL);

//Connecting To The DB & Setting utf-8
$pdo = new PDO($dsn , $user , $password , array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

在utf-8编码的PHP文件中:

<meta charset="UTF-8">

ini_set('default_charset','utf-8');
header ('Content-type: text/html; charset=utf-8');

$charset = $pdo->prepare('SET CHARSET "utf8"');
$charset->execute();

$query = 'Alfonso Castaٌeda';
utf8_encode($query);

$stmt = $pdo->prepare('SELECT * FROM locations where location = :loc").'"');
$stmt->execute(array('loc' => $query));
$values = $stmt->fetchAll(); 
var_dump($values);

运行php文件的打印代码:

array(0) { }

但是当我在本地主机上的PHPmyAdmin上执行此查询时:

SELECT * FROM locations where location = "Alfonso Castaٌeda"

我明白了:

+------------------------+
      location        id
  Alfonso Castaٌeda    2
+------------------------+

0 个答案:

没有答案