MYSQL encoding problem

I’m making an OpenAPI for my site. On my site there is a script that accesses the database (MYSQL) on a third-party site there is a javascript that accesses a query on my site and displays an image, description and title. If the text is in English, then everything works fine. But if in Russian, then on a third-party site, null appears instead of text.

Tell me how to deal with this problem?


This is a request page.

    $sql = "SELECT z, pz FROM event LIMIT 2";
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result))
        $rows[] = array(
        "z" => $row['z'],
        "p" => $row['p']);
    $json = json_encode($rows);
    $callback = $_GET['callback'];
    echo $callback.'('. $json . ')';

Answer 1, authority 100%

Try this before querying the database:

$encode = 'utf8';
mysql_query("SET NAMES '$encode';", $sqlLink);

Where $encode is the encoding. You need to put the appropriate encoding of the page (script).
And $sqlLink is a handle to connect to the DBMS server.

Answer 2, authority 100%

A better way to set the character set is to use the mysql_set_charsetfunction. However, this feature requires MySQL 5.0.7 or later.

Quote from the same page saying that using mysql_query to set the encoding is not recommended.

Using mysql_query() to execute SET NAMES .. is not recommended.