Jump to content

Insert foreign characters using PDO


Go to solution Solved by JenniferLawrence,

Recommended Posts

Hello, I want to be able to insert foreign characters into my database. However, my problem is that it is inserting the characters as question marks instead. I am using PDO as my API library. Let's say I want to insert a Japanese character like so

ハッカー

I get the following

????

I know it has something to do with setting the character set using UTF-8, however, I've tried that with the following codes below and nothing has changed.

 

Here is my Database connection.

$user = 'root';
$pass = 'root';

$options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING);
$db = new PDO('mysql:host=localhost;dbname=test;charset=utf8', $user, $pass, $options);

Here is my datbase insert.

$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);

$prepare = $db->prepare("INSERT INTO post (message) VALUES (:message);");
$parameters = array(':message' => $message);
$prepare->execute($parameters);
Edited by JenniferLawrence
Link to comment
https://forums.phpfreaks.com/topic/296681-insert-foreign-characters-using-pdo/
Share on other sites

 

Hello, I want to be able to insert foreign characters into my database. However, my problem is that it is inserting the characters as question marks instead. I am using PDO as my API library. Let's say I want to insert a Japanese character like so

ハッカー

I get the following

????

I know it has something to do with setting the character set using UTF-8, however, I've tried that with the following codes below and nothing has changed.

 

Here is my Database connection.

$user = 'root';
$pass = 'root';

$options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING);
$db = new PDO('mysql:host=localhost;dbname=test;charset=utf8', $user, $pass, $options);

Here is my datbase insert.

$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);

$prepare = $db->prepare("INSERT INTO post (message) VALUES (:message);");
$parameters = array(':message' => $message);
$prepare->execute($parameters);

Try changing

 

$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);

 

to

 

$message = filter_var($_POST['message'], FILTER_SANITIZE_SPECIAL_CHARS);

  • Solution

Try changing

 

$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);

 

to

 

$message = filter_var($_POST['message'], FILTER_SANITIZE_SPECIAL_CHARS);

Thanks, but I think I solved this with the approach I was looking for.

 

Had to delete the ;charset=utf8 part and then I had to add this line below it.

$db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES utf8");
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.