I am currently working on a project, and instead of the usual MySQL queries, I decided to go ahead and learn how to use PDO.
I have a table with the names of the contestants, the database, the table and all the columns are in utf-8. I have ten entries in the members table, and their name column contains characters such as ääö.
Now, when I retrieve a record from the database and change the name var_dump, I get a good result - a string with all the special characters intact. But I need to split the string by characters to get them in an array, which I then shuffle.
For example, I have this line: Test ÅÄÖ Tåän
And when I run str_split, I get each character in its own key in the array. The only problem is that all special characters are displayed as follows: which means that the array will look like this:
Array ( [0] => T [1] => e [2] => s [3] => t [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => T [13] => [14] => [15] => [16] => [17] => n )
As you can see, it not only spoils the characters, but also duplicates them in the str_split process. I tried several ways to break the string, but they all have the same problem. When I print a line before splitting, it displays special characters well.
This is my dbConn.php code:
// Requires a configuration file: require_once ('config.inc.php');
// Start PDO connection: $dbHandle = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=utf-8", $dbUser, $dbPass); $dbHandle -> exec("SET CHARACTER SET utf8"); // Set error reporting: $dbHandle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
And this is the code that I use to retrieve from the database and the loop:
// Require files: require_once('dbConn.php'); // Get random artist: $artist = $dbHandle->query("SELECT * FROM ".ARTIST_TABLE." WHERE id = 11 ORDER BY RAND() LIMIT 1"); $artist->setFetchMode(PDO::FETCH_OBJ); $artist = $artist->fetch(); var_dump($artist->name); // Split name: $artistChars = str_split($artist->name);
I am connecting to utf-8, my php file is utf-8 without specification, and no other special characters on this page share this problem. What could be wrong or what am I doing wrong?