1

i have some persian(utf8) words on my database for my android app and i am using json for show database information in android app.
I have following code on Databasemanager.php:

function getMusics()
{
    $connection = mysqli_connect(DatabaseManager::HOST_NAME, DatabaseManager::USER_NAME, DatabaseManager::PASSWORD, DatabaseManager::DATABASE_NAME);
    $sqlQuery = "SELECT * FROM musics where active = 1 order by date desc;";
    $result = $connection->query($sqlQuery);
    $musicsArray = array();
    if ($result->num_rows > 0) {
        for ($i = 0; $i < $result->num_rows; $i++) {
            $musicsArray[$i] = $result->fetch_assoc();
        }
    }
    echo json_encode($musicsArray);
}

and in GetMusic.php for get json information :

<?php
include "DatabaseManager.php";
$databaseManager = new DatabaseManager();
$databaseManager->getMusics();

but i have bellow jsons on output :

{"id":"3","name":"???","artist":"????? ????","like_count":"1","comment_count":"0","dl_link":null,"photo":"http:\/\/192.168.88.6\/musicarea\/photos\/3.jpg","active":"1","date":"2017-08-04 00:00:00"}

how can i solve it ??

on output(i just use persian language at 2 last rows):enter image description here on the app:
enter image description here
on php my admin:
enter image description here

9
  • have you tried setting UTF-8 as default encoding for all MySQL connections? Commented Aug 31, 2017 at 7:28
  • show how you the data in your android app Commented Aug 31, 2017 at 7:28
  • @orvenseville Data is just like that. " ????? " Commented Aug 31, 2017 at 7:31
  • @Droidman No.can you learn me how ? Commented Aug 31, 2017 at 7:31
  • @emen try using de2.php.net/manual/en/mysqli.set-charset.php Commented Aug 31, 2017 at 7:44

3 Answers 3

1

You should set the default client charset to UTF-8. Use mysqli::set_charset OR mysqli_set_charset.

See https://www.php.net/manual/en/mysqli.set-charset.php for more info.

Sign up to request clarification or add additional context in comments.

Comments

1

You have to add the header that it tells the client the response is encoding with utf-8

header('Content-Type: text/html; charset=utf-8');

5 Comments

Where should I use this? i added in bellow of <?php in both files but didnt works...
add it before the response is firing
it should be header("Content-type: application/json; charset=utf-8"); assuming that the strings stored in the database are actually utf-8 incoded strings, won't work otherwise
Sure, also the response should be received as utf8
@BasilBattikhi ok w8
1

EDIT

function getMusics()
{
    $connection = mysqli_connect(DatabaseManager::HOST_NAME, DatabaseManager::USER_NAME, DatabaseManager::PASSWORD, DatabaseManager::DATABASE_NAME);
    $sqlQuery = "SELECT * FROM musics where active = 1 order by date desc;";
    $result = $connection->query($sqlQuery);
    $musicsArray = array();
    if ($result->num_rows > 0) {
        for ($i = 0; $i < $result->num_rows; $i++) {
            $musicsArray[$i] = $result->fetch_assoc();
        }
    }
   header("Content-type: application/json; charset=utf-8");
    echo json_encode($musicsArray);
}

2 Comments

header('Content-Type: text/html; charset=utf-8'); echo json_encode($musicsArray); ??
try this one bro json_encode($musicsArray, JSON_UNESCAPED_UNICODE);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.