N
N
newdancer2015-12-09 19:17:19
PHP
newdancer, 2015-12-09 19:17:19

Problems with encoding when getting data from DB?

Problems with encoding when getting data from DB? The entire database is encoded in utf8_general_ci . When receiving data, the data is output in ancii codes. Example files:
db_connect.php

<?php
 
class DB_CONNECT {
 
    function __construct() {
        $this->connect();
    }
 
    function __destruct() {
        $this->close();
    }
 
    function connect() {
        require 'db_config.php';
 
        $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
mysql_query('SET NAMES utf8 COLLATE utf8_general_ci', $con);
        $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
 
        return $con;
    }

    function close() {
        mysql_close();
    }
 
}
 
?>

get_all_cinema.php
<?php
 
$response = array();

require 'db_connect.php';
 
$db = new DB_CONNECT();

$result = mysql_query("SELECT *FROM dle_post") or die(mysql_error());
 
if (mysql_num_rows($result) > 0) {
    $response["dle_post"] = array();
 
    while ($row = mysql_fetch_array($result)) {
        $cinema = array();
        $cinema["pid"] = $row["id"];
        $cinema["title"] = $row["title"];
        $cinema["category"] = $row["category"];
        $cinema["short_story"] = $row["short_story"];
        $cinema["xfields"] = $row["xfields"];
 echo $row["title"];
        array_push($response["dle_post"], $cinema);
    }
    $response["success"] = 1;
 
    echo json_encode($response);
} else {
    $response["success"] = 0;
    $response["message"] = "No cinema found";
 
    echo json_encode($response);
}
?>

without a string it was displayed before "null", now ancii code
mysql_query('SET NAMES utf8 COLLATE utf8_general_ci', $con);

sample output from the link: kinoobzor.org/android/get_all_cinema.php

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrew, 2015-12-09
@misterfil

mysql_set_charset('utf8',$con);
after

$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());

S
sivabur, 2015-12-09
@sivabur

Google mysql ubuntu setup in the first result everything is clear.
PS Practically always these are the server settings. Moreover, it is not alone, but in my opinion in 3 places it is necessary to change.

N
newdancer, 2015-12-09
@newdancer

shared everything. left only with img. For example, there are three images
how to extract 3 links from here and write them separated by commas?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question