I have a problem with json_encode, I have a database that is encoded in utf8mb4_unicode_ci which contains accented French characters.
when I do the select and I encode in json (json_encode) I have no display but when I remove the accents it works, if someone can help me.
Thanks
getData.php
<?php
include "connection.php";
//include "crypto.php";
header('Content-Type: application/json');
$stmt = $db->prepare("SELECT * FROM ec_product_categories");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($result);
?>
Related
honestly I am coming here after trying everything I could find online in order to fix that problem but nothing worked..
I have a phpmyadmin database table that encoded to utf_general_ci, when I insert data in Hebrew into it it works fine and I can see the Hebrew in the table on PhpMyAdmin, but when I need to retrieve the Hebrew data from the table that's when the problem starts...
right now I am using a simple sql query to select data from the table and I tried every code that should fix it I could find online but nothing seems to work.
my current code is:
<?php
$db = "android";
$username = "root";
$password = "";
$host = "localhost";
$sql = "select * from sample;";
$conn = mysqli_connect($host,$username,$password,$db);
$conn->set_charset('utf8');
$result = mysqli_query($conn,$sql);
$response = array();
while($row = mysqli_fetch_array($result))
{
array_push($response,array($row[0],$row[1]));
}
$str = json_encode(array($response));
echo $str;
mysqli_close($conn);
?>
and the output of the Hebrew word is \u05d4\u05d9\u05d9 what seems like a different encoding, my php file is encoded to UTF-8 and I am using npp to write it.
please try helping me out on this one cause I couldn't find an answer
This is a json_encode() behaviour, by default Unicode char are escaped as \u...
Try with JSON_UNESCAPED_UNICODE flag :
<?php
echo json_encode($arr, JSON_UNESCAPED_UNICODE);
I am having trouble with the output of json_encode. I need to output Russian characters.
In my database table only Russian characters. In the output I am getting only "????????" question marks replaced Russian characters. I read many similar questions but none of them offered a real solution. I tried the following but none of them helped.
Below is my php code.
added ``header ('Content-type: application/json; charset=utf-8');`
used json_encode($albums, JSON_UNESCAPED_UNICODE);
tried mb_convert_encoding($str, 'UTF-8', 'auto');
json_encode($albums, JSON_UNESCAPED_UNICODE);
<?php
$host ="localhost";
$user ="misollar_user";
$pass="12345";
$db="misollar_db";
header ('Content-type: application/json; charset=utf-8');
$con = mysqli_connect($host,$user,$pass,$db);
$query = "select * from albums;";
$result = mysqli_query($con, $query);
$albums = array();
while ($row = mysqli_fetch_array($result)){
array_push($albums,array('id'=>$row[0], 'name'=>$row[1], 'songs_count'=>$row[2]));
}
mysqli_close($con);
echo json_encode($albums, JSON_UNESCAPED_UNICODE);
?>
You need to set UTF8 before retrieving results from mysql.
Just before you retrieve results from albums table, fire below query:
mysqli_query($con, 'SET names UTF8');
After this you can fetch your album results:
$query = "select * from albums;";
$result = mysqli_query($con, $query);
There are a lot of topics who discuss my problem but I cant still solve it.
I have a database which I encode by JSON. But when I put content with german char like ä, ö, ü etc the output is empty.
Here is my php script:
<?PHP
error_reporting(E_ALL);
ini_set('display_errors', '1');
//Verbindung zur Datenbank
include "inc_mysql.php";
//SQL-String
$select = "SELECT * FROM module ORDER BY name ASC";
//SQL-Befehl in Variable speichern
$sql = mysql_query($select);
//********************this one was missing!********************
mysql_set_charset("utf8");
//Array erstellen
$jsonArray = array();
while ($row = mysql_fetch_array($sql)){
$jsonArray[] = $row;
}
echo json_encode($jsonArray, JSON_UNESCAPED_UNICODE);
?>
The php file is UTF-8 without BOM.
The column is the dabase is utf8_general_ci.
BTW: The error reporting just says something about mysql_query() is depricated.
Might be a easy game for you guys :)
Solution found! Check the code!
You can use json_last_error_msg() function to show the error message. http://php.net/manual/en/function.json-last-error-msg.php
This function mb_convert_encoding() may help you.
core.php
$con=mysqli_connect("localhost","root","","mmogezgini");
header('Content-Type: text/html; charset=utf-8');
gamelisti.php
$result = mysqli_query($con,"SELECT * FROM games");
while($row = mysqli_fetch_array($result))
{
?>
HTML CODES
<?php echo $row['game_name']; ?>
HTML CODES
<?php }
mysqli_close($con);
?>
i use turkish characters like "ö,ç,ğ,ı" i see these correctly in database but when i select them from database and show with php echo they looks like question mark=(?)
my encoding on database utf8_general_ci
Double check your HTML encoding.
http://php.net/manual/en/mysqli.set-charset.php (just after you connect to DB: mysqli_set_charset($con, 'utf8');
Are you using prepared statements? (not related but important).
I'm using MySQLi to retrieve information from the database. All of my pages are encoded with UTF-8 without BOM, i have 2 pages to deal with, but the main problem is that on the database the information is showed in a regular way - but when i retrieve it it goes like this:
����� ����� - ���?
on the phpMyAdmin panel - the columns are latin1_general_ci (i tried to make it utf8_general as well - same output!).
the "mysql.php":
<?php
header('Content-type: text/xhtml; charset=windows-1255');
$mysql = new mysqli('localhost', 'elenbyin_vadim', 'pass33323', 'elenbyin_vadim');
?>
the "index.php", main parts:
<?php
include ('mysql.php');
include('functions.php');
if($_GET["lang"] == 'rus')
$lang = 'rus';
else if($_GET["lang"] == 'heb')
$lang = 'heb';
else
$lang = 'heb';
......
<div class="main_text">
<?
$result = $mysql->query('SELECT * FROM homepage');
while($row = mysqli_fetch_array($result)){
echo "".$row['title']."<br />".$row['text_heb']."";
}
?>
</div>
can someone help my to solve this problem?
You'll have to change the charset for it to work:
<?php
$mysql->set_charset('utf8');
$result = $mysql->query('SELECT * FROM homepage');
...
?>
#Class's comment - If you need it in all your pages you can add it to mysql.php:
<?php
header('Content-type: text/xhtml; charset=windows-1255');
$mysql = new mysqli('localhost', 'elenbyin_vadim', 'pass33323', 'elenbyin_vadim');
$mysql->set_charset('utf8');
?>
Also, make sure you set your collation to utf8_general_ci.
An older method will be to set the names to utf8:
<?php
$mysql->query("SET NAMES 'utf8'");
$result = $mysql->query('SELECT * FROM homepage');
...
?>
However according to set_charset() docs the first is preferrable:
Note: This is the preferred way to change the charset. Using
mysqli_query() to set it (such as SET NAMES utf8) is not recommended.
See the MySQL character set concepts section for more information.