JenniferLawrence Posted June 6, 2015 Share Posted June 6, 2015 (edited) 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 June 6, 2015 by JenniferLawrence Quote Link to comment Share on other sites More sharing options...
jamesmpollard Posted June 6, 2015 Share Posted June 6, 2015 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); Quote Link to comment Share on other sites More sharing options...
Solution JenniferLawrence Posted June 7, 2015 Author Solution Share Posted June 7, 2015 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"); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.