This question already has answers here:
php json_encode not working on arrays partially
(2 answers)
Closed 6 years ago.
When I try to encode on a JSON a query from MySQL database that gives me a null JSON when the words have accents eg. " olá " , " à " etc
my php code:
$sql = "SELECT `name`
FROM `login`
WHERE `id`='1';";
$result = mysqli_query($conn, $sql);
$emparray = array();
while($row = mysqli_fetch_assoc($result)){
$emparray[] = $row;
}
echo json_encode($emparray,JSON_UNESCAPED_UNICODE);
my database:
id | name |
1 | Olá |
json_encode assumes that the result is encoded in UTF-8, if it is not returned null
first verify that your database server is configured with UTF_8
or add the set_charset before the consultation
mysqli_set_charset($conn,"utf8");
Use header to modify the HTTP header, UTF-8 all the way through
header('Content-Type: text/html; charset=utf-8');
Its working fine check Click Here
<?php
echo json_encode("Olá ",JSON_UNESCAPED_UNICODE);
?>
Related
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 6 years ago.
Hey so i'm getting text with latin characters like "ó" from a mysql database. This is the connection along with the query i'm using to get the data, placing it in an array:
<?php
header('Content-Type: text/html; charset=UTF-8');
$con=new mysqli ("localhost","root","","chroniclemark");
$sqlget = "SELECT descricao FROM items;";
$sqldata = mysqli_query($con,$sqlget) or die ('error getting database');
while ($row = mysqli_fetch_array($sqldata, MYSQLI_ASSOC)) {
$lol5[] = $row['descricao'] ;
}?>
I'm then trying to display the the text outside of the <?php ?> statement like so:
<p ><?php echo $lol5[0]; ?></p>
All of the text is fine except for the "ç", "ó", "ã" and so on.
I also have a meta tag <meta charset="UTF-8"> in my header but it doen't help.
In the database, the data is set to "utf8mb4_unicode_ci".
What can i do to fix this? Any idea?
#Fred -ii Thanks for pointing me in the right direction in the comments.
All i had to do was use mysqli_set_charset($con, 'utf8mb4'); before making the query
$sqlget = "SELECT descricao FROM items;";
$sqldata = mysqli_query($con,$sqlget) or die ('error getting database');
It's all fine now :)
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 7 years ago.
I can connect to DB but i cant echo , all time that said 0 results but my DB have TABLE (text) with 2 COLUMN id and text and i have inserted text in my table.
$con = mysqli_connect("localhost","1078362","nenaddurmisi022");
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT vesti FROM comment";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0)
{
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo $row["vesti"]."<br>";
}
}
else {
echo "0 results";
}
mysqli_close($con);
You're using all mysqli functions except for your database connection. Try changing the first line to...
$con = mysqli_connect("localhost", "1078362", "nenaddurmisi022");
...and change the last line to...
mysqli_close($con);
I'm guessing there are a lot of errors generated by this script, but you're not seeing them. You should add these lines to the top of the script...
error_reporting(E_ALL);
ini_set('display_errors', 1);
try
$sql = "SELECT * FROM text";
instead of
$sql = "SELECT text FROM text";
also be sure you'r table name is text.
one last thing you could try is to replace
echo $row["text"]."<br>";
by
print_r($row);
to see if there's a result set from you'r Database
This question already has answers here:
UTF-8 all the way through
(13 answers)
Non English characters appear as question marks on my php page - appear fine in database
(2 answers)
Closed 7 years ago.
I have saved my data on MYSQL database using "utf8_general_ci". I wrote it on Bangla font.
But when I am trying to show data using php it is showing "????..." instead of showing the charecters.
<?php
$con = mysqli_connect("localhost","appsaadr_edu","pass","appsaadr_edu");
$result = mysqli_query($con,"SELECT * FROM lnews ORDER BY id DESC");
echo "{\"news\":";
$arr = array();
while($rows = mysqli_fetch_array($result)){
$test['id'] = $rows['id'];
$test['title'] = $rows['title'];
$test['text'] = $rows['text'];
$test['time'] = $rows['time'];
array_push($arr,$test);
}
echo json_encode($arr);
echo "}";
?>
What to do now?
Make sure the file is encoded in UTF-8 without BOM
Add : mysqli_set_charset($con,"utf8"); between the lines 3 and 5
Put all the $test['XXX'] like this utf8_encode($test['XXX'])
Make sure to add the data into the database also through utf8_encode before
This question already has answers here:
JSON - slashes not escaping
(2 answers)
Closed 9 years ago.
This my PHP script:
<?php
header('Content-Type: application/json; charset=utf-8');
$link = mysql_pconnect("localhost", "test", "test") or die("Could not connect");
mysql_select_db("myradio") or die("Could not select database");
$arr = array();
$rs = mysql_query("SELECT * FROM radio1");
while($obj = mysql_fetch_assoc($rs)) {
$arr[] = $obj;
}
echo '{"success":true,"error":"","data":{"schedule":['.json_encode ($arr).']}}';
?>
The JSON displays well, however, slashes are not escaped, which results in:
It's the weekend
when it should be:
It\'s the weekend
within the JSON.
Also how can I manipulate my PHP/JSON so that depending on callback, it gives this error message:
({"success":false,"error":"File does not exist"});
It's working well so far, just need to get the finer details right, would appreciate some help!
As for my PHP, I am using PHP 5.4.10 on a MAMP server, if that's of any relevance.
Read this article with attention. It describes how you can escape special symbols like '\' by encoding them with different options like JSON_UNESCAPED_UNICODE in the json_encode function.
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 8 years ago.
I'm trying to print values from the database, containing countrylist. One of the fields in this database is cyrilic. My PHP script looks like this:
include_once("db.php");
$query = mysql_query ("SELECT * FROM country");
$row = mysql_fetch_array($query);
do {
$id = strtolower($row["id"]);
$value = ($row["runame"]);
echo $id . " " . $value . '<br/>';
} while ($row = mysql_fetch_array ($query));
The output from this script looks like this:
ad ???????
ae ???????????? ???????? ???????
af ??????????
ag ??????? ? ???????
ai ????????
So, apparently I need to set UTF-8 for this PHP script... somehow... Does anyone know how to do this?
Thank you for your time!
Try using SET NAMES utf8 after connecting to MySQL:
mysql_query("SET NAMES utf8");
Or if you are using cp1251 in the database, set this encoding.
As the manual says:
SET NAMES indicates what character set the client will use to send SQL
statements to the server... It also specifies the character set that the server should
use for sending results back to the client.