0

I'm working on a script that retrieves values from a MySQL database for each individual letter within a $word. The script works great, but not for Latin characters like an enye.

Example Code:

$word = "Cañones";

$letters = str_split($word);

print_r($letters);

$query = "SELECT GROUP_CONCAT(Alpha) AS Alpha,
             GROUP_CONCAT(Letter_Box_Width) AS Letter_Box_Width
      FROM Font_Krinkes
      WHERE Alpha IN ('" . implode("','", $letters) . "')";

Output of print_r

Array
(
    [0] => C
    [1] => a
    [2] => ?
    [3] => ?
    [4] => o
    [5] => n
    [6] => e
    [7] => s
)
PHP Notice:  Undefined index: ?
PHP Notice:  Undefined index: ?

It seems to me that the enye is being stored and retrieved from my $letters array incorrectly. It's being retrieved as a question mark... Then, of course, the query for the question mark value fails, and this is why I'm getting the Undefined index error. How do I deal with this enye character within my array?

After working through the comments, I realize my code has more issues than just the mb_str_split issue. I'm still unable to query the database correctly.

Any help is much appreciated.

Wyatt Jackson
  • 303
  • 1
  • 2
  • 11
  • What's your database encoding and collation ? – msg Oct 09 '18 at 23:50
  • UTF-8 and utf8_bin Not sure how this pertains to the array. The Latin characters are stored fine in the database. – Wyatt Jackson Oct 09 '18 at 23:54
  • Oh, so they are correcly stored in the database. But looks like php treats them as digraphs. Not sure `str_split` works with multibyte, have you checked [the reference](http://php.net/manual/en/function.str-split.php)? – msg Oct 10 '18 at 00:00
  • You are correct! "str_split() will split into bytes, rather than characters when dealing with a multi-byte encoded string." – Wyatt Jackson Oct 10 '18 at 00:06
  • [This](http://php.net/manual/en/function.str-split.php#V117112) seems to work, as does the next one – msg Oct 10 '18 at 00:10
  • Ok. Thank you. I have made progress. The mb_str_split stores the ñ correctly in the array. print_r displays the ñ and no more question marks. But, I'm still getting the "Undefined index: ?" errors. I also checked the implode function, and it also outputs the ñ correctly... – Wyatt Jackson Oct 10 '18 at 00:26
  • Possible duplicate of [PHP: Split multibyte string (word) into separate characters](https://stackoverflow.com/questions/2556289/php-split-multibyte-string-word-into-separate-characters) – miken32 Oct 10 '18 at 00:28
  • Also, just to make sure the DB encoding was fine... I tested a simple query of "select * from Font_Krinkes where Alpha ="ñ";" and it returned the row just fine... – Wyatt Jackson Oct 10 '18 at 00:29
  • This is not a duplicate question. – Wyatt Jackson Oct 10 '18 at 00:36

1 Answers1

0

In storing ang getting string data from your database and making sure all characters display well, you need to set your connection's character encoding. Simply add this line: mysqli_set_charset($conn,"utf8"); wherein your $conn is your connection to the database.

Erlisar Vasquez
  • 460
  • 3
  • 13
  • I was so hopeful this would work, but unfortunately, this did not work either. I'm still getting the same "PHP Notice: Undefined index: ?" error when including the ñ – Wyatt Jackson Oct 10 '18 at 01:12