Cyrillic displays as ??? ???? in PHP [duplicate] - php

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.

Related

Can't get NON-english letters(text) from MySQL in PHP

I am android developer and using PHP just for getting data from MySQL (so my PHP is very poor) and it always works but I come across that I can't get data with non-english letters
I have written that kind of question How to put in JSON Object non-english letters?
but in that question I thought that problem was in json encoding, but it was not problem in json.(but now I know how to solve problem with json)
so I need just to get non-english text data from MySQL
I thought that problem was in database so I changed everything to utf-8 , checked twice, made the same query in database and get expected result, so problem is in php code.
then I researched and thought that maybe my php didn't maintain utf-8, and that line <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> to the top then create a simple String variable that consist non English letters and echo that variable, and it has showed me what I expect, so I think php support utf-8
so I think the only problem is in the part with mysqli_query
that is my code :
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<?php
header("Content-type: text/html; charset=utf-8");
$con = mysqli_connect(...);
$sql = "SELECT * FROM news WHERE status = 'actual' ORDER BY id DESC";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - TITLE: " . $row["title"]. " -TEXT: " . $row["text"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($con);
?>
it is my data base datas:
and structure:
for all database
MySQL charset: UTF-8 Unicode (utf8)
MySQL connection collation: utf8_unicode_520_ci
I expected to get the same datas as in title and text columns (you can see datas above)
but I get:
id: 4 - TITLE: ???????? -TEXT: ????????? ??????? ?????, ? ??? ????????? ?? ?????????
id: 3 - TITLE: A little bit... -TEXT: Just some more patience! And you will get this wonderfull application!
id: 1 - TITLE: �p�???�? -TEXT: Just wait a little bit!
I tried to write with all details of my problem, so help me with code, what I should add/modify (in which line?) to get expected result.
Thank you all in advance!
SOLUTION HAS BEEN FOUND
I should add
$con->set_charset("utf8");
before declaring $sql

Null php json while econde a query from mySQL with accents [duplicate]

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);
?>

Showing utf8_general_ci data (non-eng) on php [duplicate]

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

Arabic not supported in mysql and php [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 9 years ago.
I have this part of code that works fine only the arabic issue.
$result = mysql_query("SELECT login, password, name, role FROM qm_users WHERE login = '$login'");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
$row = mysql_fetch_array($result);
echo $row["name"];
The name row contains arabic name.
The output of echo is ???? ???? ????
In my myphpadmin of xamp server, here the structure
Check below possible solution,
1) Your table structure 'Collation' must be 'utf8_unicode_ci'.
2) Also try to set below thing in php file
ini_set('default_charset','utf-8');
mysql_set_charset('utf8');
header('Content-type: text/html; charset=utf-8');
If you are going to save UTF8 encodings to your MySQL database first set your connection encoding to UTF8.
<?php
$connection = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
mysqli_set_charset($connection, "utf8");
?>

Problems in inserting utf-8 string into database and then outputting it to web page

I am learning PHP programming, so I have setup testing database and try to do various things with it. So situation is like that:
Database collation is utf8_general_ci.
There is table "books" created by query
create table books
( isbn char(13) not null primary key,
author char(50),
title char(100),
price float(4,2)
);
Then it is filled with some sample data - note that text entries are in russian. This query is saved as utf-8 without BOM .sql and executed.
insert into books values
("5-8459-0046-8", "Майкл Морган", "Java 2. Руководство разработчика", 34.99),
("5-8459-1082-X", "Кристофер Негус", "Linux. Библия пользователя", 24.99),
("5-8459-1134-6", "Марина Смолина", "CorelDRAW X3. Самоучитель", 24.99),
("5-8459-0426-9", "Родерик Смит", "Сетевые средства Linux", 49.99);
When I review contents of created table via phpMyAdmin, I get correct results.
When I retrieve data from this table and try to display it via php, I get question marks instead of russian symbols. Here is piece of my php code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Books</title>
</head>
<body>
<?php
header("Content-type: text/html; charset=utf-8");
mysqli_set_charset('utf8');
# $db = new mysqli('localhost', 'login', 'password', 'database');
$query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
$result = $db->query($query);
$num_results = $result->num_rows;
for ($i = 0; $i < $num_results; $i++) {
$row = $result->fetch_assoc();
echo "<p><strong>".($i+1).". Title: ";
echo htmlspecialchars (stripslashes($row['title']));
echo "</strong><br />Author: ";
echo stripslashes($row['author']);
echo "<br />ISBN: ";
echo stripslashes($row['isbn']);
echo "<br />Price: ";
echo stripslashes($row['price']);
echo "</p>";
}
...
And here is the output:
1. Название: Java 2. ??????????? ????????????
Автор: ????? ??????
ISBN: 5-8459-0046-8
Цена: 34.99
Can someone point out what I am doing wrong?
Can someone point out what I am doing wrong?
Yes, I can.
You didn't tell Mysql server, what data encoding you want.
Mysql can supply any encoding in case your page encoding is different from stored data encoding. And recode it on the fly.
Thus, it needs to be told of client's preferred encoding (your PHP code being that database client).
By default it's latin1. Thus, because there is no such symbols in the latin1 character table, question marks being returned instead.
There are 2 ways to tell mysql what encoding we want:
a slightly more preferred one is mysqli_set_charset() function (method in your case).
less preferred one is SET NAMES query.
But as long as you are using mysqli extension properly, doesn't really matter. (though you aren't)
Note that in mysql this encoding is called utf8, without dashes or spaces.
Try to set output charset:
SET NAMES 'utf-8'
SET CHARACTER SET utf-8
Create .htaccess file:
AddDefaultCharset utf-8
AddCharset utf-8 *
CharsetSourceEnc utf-8
CharsetDefault utf-8
Save files in UTF-8 without BOM.
Set charset in html head.
After your mysql_connect, set your connection to UTF-8 :
mysql_query("SET NAMES utf8");
Follow Alexander advices for .htaccess, header and files encoding
You probably need to call mysqli_set_charset('utf8'); after you set up your connection with new mysqli(...) as it works on a link rather than a global setting.
so..
# $db = new mysqli('localhost', 'login', 'password', 'database');
mysqli_set_charset($db, 'utf8');
$query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
By the way, that query seems to be open to SQL-injection unless $searchterm is sanitized. Just something to keep in mind, consider using prepared statements.
And using # to suppress errors is generally not recommended, especially not during development. Better to deal with error-conditions.
after your mysql_query add
#mysql_query("SET character_set_server='utf8'; ");
#mysql_query("SET character_set_client='utf8'; ");
#mysql_query("SET character_set_results='utf8'; ");
#mysql_query("SET character_set_connection='utf8'; ");
#mysql_query("SET character_set_database='utf8'; ");
#mysql_query("SET collation_connection='utf8_general_ci'; ");
#mysql_query("SET collation_database='utf8_general_ci'; ");
#mysql_query("SET collation_server='utf8_general_ci'; ");
Try to put also in the HTML document Head the meta tag:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
this is different to the HTTP header header("Content-type: text/html; charset=utf-8");

Categories