Search code examples
phpmysqljsonspecial-characters

mysql left retrieve first x characters and json_encode utf8


i have a problem with this query:

Select LEFT(name,3) 
from table
where id = '1'

the query is ok, but when the name has some special characters that get's cut i can't json_encode() it, so my question is how can i cut the name with a mysql query so i get whole special caracters so i can json_encode it.

on json_last_error() i get "5" that is JSON_ERROR_UTF8 because the character got cut and is not utf8 anymore...

my name field is varchar(255) latin1_swedish_ci and the name i am trying to cut is like: "șÂȚâț"

Can i use LEFT() for this query or do i have to use php to only show first 3 chars. Thanks.


Solution

  • I managed to solve it by setting

    mysqli_set_charset($sql,'utf8mb4');

    after my initial mysqli_connect

    and i had to change my table Collation to utf8mb4_general_ci. Any utf8 worked but this new utf8mb4 seems to be the future most used Collation and i don't need sorting since i will only get just one name out :)

    Thanks