Can't get NON-english letters(text) from MySQL in PHP - 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

Related

retrieving data in Indian language from MySql database using php shows unusual character set

I am retrieving data in Indian language and I am getting output as ??????????????????????????
NOTE : - I used this 2 line in my php script
header('Content-type: text/plain; charset=utf-8');
mysqli_set_charset($conn,'utf-8');
and also I used utf8_general_ci encoding in databse and table.
my php script is
<?php
require 'init.php';
header('Content-type: text/plain; charset=utf-8');
mysqli_set_charset($conn,'utf-8');
$table = $_GET['table_name'];
$sql = "SELECT * FROM ".$table;
$response = array();
$result = mysqli_query($conn,$sql);
if(mysqli_num_rows($result)>0) {
while($row = mysqli_fetch_array($result)) {
array_push($response, array('id'=>$row['id'],'text'=>$row['text']));
}
echo json_encode($response);
} else {
echo "error";
}
mysqli_close($conn);
?>
You can check the link http://www.sharefb.com/statusApp/readStatus.php?table_name=gujrati_attitude
Use mysqli_set_charset( $db, 'utf8');
Add this meta tag in header and try
<META HTTP-EQUIV="Content-Type" CONTENT="text/html"; charset="utf-8" />
Note : If the result shows like the ???? then u may need to install the external language support tool by enabling the following options,
Control Panel -> Regional and Language Option -> Languages -> Install files for complex script and right-to-left languages (including Thai).
See "question marks" in Trouble with utf8 characters; what I see is not what I stored
If you still have trouble, provide
SHOW CREATE TABLE
Sample of the text being inserted
SELECT HEX(...) of a sample of the bad data (? = 3F)

Store and output text with accents

I have some text in a database. I use French and English. French has accents, and some special characters like ç. I use Mamp, MySQL and PHP.
I have collation latin1_swedish-ci (the default). I tried utf8_general_ci and the result is the same.
If I use in a html page, I have this in the head: <meta charset="UTF-8">
As an example, in the database I have "voilà".
When I echo the text from the database to html:
$con = mysqli_connect("localhost","root","root");
if (!$con) {
die('The connexion failed: ' . mysqli_error());
}
if (!mysqli_select_db($con, 'prova')){
echo "Connection with database was not possible";
}
$result = mysqli_query($con, "SELECT * FROM test1
WHERE id='1' ")
or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
$text = $row['first'];
echo $text; //I see: voil�
echo htmlentities($text); //I see nothing
echo utf8_encode($text); //This works: I see voilà
}
Why htmlentities does not work?
Is utf8_encode(); the way to go? I have to use that always when I output something from the database? Why do I have to use that if the collation is already UTF8? Is there any better way to store and output text with accents in a MySQL database?
After you connect to the DB you should set the client charset to UTF8:
mysqli_set_charset($con, "UTF8");
Otherwise the mysql client transforms the UTF8 'voilà' to latin1 ('cause it seems that is it's default).
Either you tell the client that I want everything in UTF8, or you get it with the default latin1, and convert it one-by-one yourself calling utf8_encose($text)

I can't get Chinese characters from MYSQL to PHP

I found a lot of answers for my problem but nothing worked.
I have Chinese characters in my MYSQL database and when I SELECT my field in php I get '???' instead of '我们的产品'. I was able to INSERT this characters from php to my MYSQL database.
With characters like 'éà' I have no problem to get them in PHP.
My database, table and field are encoded in utf8_unicode_ci. There is my php file(also encoded in utf-8) :
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<?php
$query = mysql_query("SELECT * FROM table");
$arr = mysql_fetch_array($query))
echo ($arr['field']);
?>
If I replace my code like this it prints 我们的产品 :
$query = mysql_query("SELECT '我们的产品'");
arr = mysql_fetch_array($query)
echo ($arr[0]);
I hope I gave enough information.
set charset as utf8 before making a mysql query, like this
mysql_connect('localhost','mysql_user','your_password');
mysql_select_db('my_db') or die( "Unable to select database");
mysql_set_charset('UTF8');
mysql_query('Your query here');

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

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.

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