0

Here is the table :

//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"));

In the PHP file which is utf-8 encoding:

<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);

The printed code on running the php file:

array(0) { }

But when I execute this query on PHPmyAdmin on my localhost:

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

I get :

+------------------------+
      location        id
  Alfonso Castaٌeda    2
+------------------------+
jack
  • 151
  • 2
  • 11

0 Answers0