0

I'm creating a database this way:

    $db=new PDO('mysql:host='.$db_host.';dbname='.$db_name.';charset=utf8',$db_user,$db_pass,array(
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

and at some point I want to insert a string in a table containing strange characters, using prepared statements. for example

    $title='vélo';
    $db->prepare('INSERT into table (title) VALUES(:title)');
    $db->execute(array('title' => $title));

This causes me the string to be stored as vélo, even though the whole database,table and entries are encoded as utf8_general_ci.

1)Could I avoid this transformation?

2)If not, how could I then do a case-insensitive search in my database? using

    SELECT * FROM table WHERE title LIKE '%velo%'

does not give me any results...

repptilia
  • 457
  • 8
  • 19
  • That code will certainly *not* store an **HTML encoded** "vélo" in the database. No database driver will ever HTML encode anything automatically. Likely you're getting the data from the browser HTML encoded, because you have not correctly instructed it how to handle encodings. See the linked duplicate. – deceze May 18 '14 at 10:13
  • I have trouble finding out what's wrong. When executing the command $db->query('INSERT into table (title) VALUES(\'vélo\')'); I get the result i'm expecting... – repptilia May 18 '14 at 10:47
  • Uhm, read my above comment again, please? – deceze May 18 '14 at 10:49
  • Thank you for your answer. I got my problem straightened out! – repptilia May 18 '14 at 11:20

0 Answers0