Answer the question
In order to leave comments, you need to log in
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();
}
}
?>
<?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);
}
?>
mysql_query('SET NAMES utf8 COLLATE utf8_general_ci', $con);
Answer the question
In order to leave comments, you need to log in
mysql_set_charset('utf8',$con);
after
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question