PHP utf8_decode works great except when saving in MySQL database - php

I'm reading data from a Access database with PHP & ODBC driver to insert into MySQL. It works nicely, except with data containing accents.
I use the following to retrieve the data from a Access Table:
$conn = new \PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ={$dbPath}; Uid={$dbUser}; Pwd={$dbPass};");
$stmt = $conn->prepare('SELECT * FROM users');
$stmt->execute();
When looping through results I have a field named name which holds the user name.
$name = $row['name'];
The output of this value into the screen shows:
Jo�o
The correct word is "João". When saving in the MySQL database it saves Jo?o.
If I output the value into the screen using the utf8_decode works great, it shows João.
print_r(utf8_decode($name));
But when saving using the same function it saves once again Jo?o.
I have also tried to save the data using htmlspecialchars(utf8_decode($name), ENT_QUOTES, 'UTF-8') but the value is saved as empty.
How do I insert into MySQL database the correct value "João"? My MySQL table has the collation utf8_bin.

Solved.
By using the mb_convert_encoding along side with utf8_encode the value is saved correctly in the database.
mb_convert_encoding(utf8_encode($name), 'UTF-8', 'UTF-8')

Related

PHP - insert Unicode data into MYSQL formatted properly

I am inserting some data from a sjson file into mysql database. Some of the data is formatted using unicode.
When I echo the data on the browser is showing fine however, it is inserted in the database not formatted properly.
In the database I am using as Collation utf8_unicode_ci.
JSON data:
anch\u2019io, sar\u00e0
Showing in the Browser:
anch'io, sarà
Showing in the mysql database:
anch’io, sarÃ
How can I inserted in the database the text properly formatted?
PHP
$getUrl = "https://example.com/79809000.json";
$json_level = file_get_contents($getUrl);
$data_level = json_decode($json_level);
$text = $data_level->{"text"};
mysqli_query($conn, "INSERT INTO `70_level`(`text`) VALUES ('$text')");
I have tried to use addslashes, htmlentities but it does not work.
You are probably not using utf8 as your character set connection/collation connection.
The easiest way to fix this will be to use
$conn = mysqli_connect(...);
mysqli_query($conn, "SET NAMES 'utf8'");
The SET NAMES query should be the first query you run, so make sure you put it right after your mysqli_connect function.
Note that this change will affect all the data, so if you already have data in the database - you will need to re-insert it using the new (utf8) charset.

Urdu / Arabic font data from MySQL is displaying as ????? in JSON

I am developing an Android app with Urdu/Arabic data store in MySQL database on my web server and using JSON_Encoding to generate the JSON string. The JSON string is then being used in Android app to perform various functions (populating RecyclerView and other view objects with data). I am able to store Urdu / Arabic data in MySQL database, but when I use PHP script to generate JSON, all the fields containing Urdu characters is displaying data as ??????
I was using the utf8mb4_unicode_ci as I read the this is easy for storing non-English data and performing multiple functions, but after this encoding problem, I have changed that to utf8_general_ci for all the tables and fields in MySQL database. Below is the PHP script I am using to generate the JSON string from MySQL:
<?php
require "conn.php";
mysqli_query("SET NAMES 'utf8'");
mysqli_query('SET CHARACTER SET utf8');
$sql_qry = "SELECT * FROM countrybasic;";
$result = mysqli_query($conn, $sql_qry);
$response = array();
while($row = mysqli_fetch_array($result)){
array_push($response, array("id"=>$row[0],"name"=>$row[1],"capital"=>$row[2],"continent"=>$row[3],"population"=>$row[4],"gdp"=>$row[5]));
}
echo json_encode(array("server_response"=>$response));
mysqli_close($conn);
?>
The Name and Capital fields are the ones I store my Urdu data in.
Please help me out to resolve this issue.
Thanks.
Create your table [countrybasic] with collation utf8mb4_unicode_ci and make the name column with the same colation.
Now insert some sample data in different languages.
Get the data using MySQLi query result.
Note: If you save the data when the collation is different and get that one after changing the collation then that data will not fetch correctly.
I hope this will work.
You just have to change the Charset to UTF8, and you can use these lines for PHP to do it:
$statSQL= 'SET CHARACTER SET utf8';
mysqli_query($your_db,$sSQL)
or die ('charset in DB didn\'t change');
I hope this help :)
$CONNECTION = mysqli_connect($host,$user,$password,$database);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query ($CONNECTION ,"set character_set_results='utf8'");
$queryutf8 = "select * from yourtable";
$res_utf8 = mysqli_query($CONNECTION ,$queryutf8 );
You need to change the default utf8 in the wamp server
check the below link for more detail.
Arabic characters doesn't show in phpMyAdmin

Retrieve multi language text from DB in php

I am using MySQL phpmyadmin.
I have a inserted a multi-lingual text into DB the Collation is utf8_unicode_ci now i want to retrieve the values from DB and to store into a file. while i reading the text it returns ?????????? but the original text is தமிழ்.
i have written the text directly to the file and it is stored the text as தமிழ்தமிழ் (some square brackets), but while reading from DB since it is returning as ?????????? the file has been return as ??????????.
How can i read from DB correctly the text as it is.
Try sending the following query after connecting to the database:
If you're on mySQL < 5.0.7:
mysql_query("SET NAMES utf8;");
if you're on a newer mySQL:
mysql_set_charset("utf8");
AND
header('Content-type: text/plain;charset=UTF-8');
Try to execute 'SET NAMES utf8;' before the reading table.

Why do I get invalid characters when converting MS SQL Data to MYSQL?

I'm writing a PHP script to import data into a MYSQL database from a Microsoft SQL Server 2008 database.
The MSSQL Server is set with a collation of "SQL_Latin1_General_CP1_CI_AS" and the data in question is being stored in a column of the type "nchar".
My PHP web pages use
<meta http-equiv="content-type" content="text/html; charset=utf-8">
to indicate that they should be displayed with UTF-8 Character encoding.
I'm pulling the data from the MSSQL database using the sqlsrv PHP extension.
$sql = 'SELECT * FROM [tArticle] WHERE [ID] = 6429';
$stmt = &sqlsrv_query($dbHandler, $sql);
while ($row = sqlsrv_fetch_object($stmt)) {
// examples of what I've tried simply to display the data
echo $row->Text1;
echo utf8_encode($row->Text1);
echo iconv("ISO-8859-1", "UTF-8", $row->Text1);
echo iconv("ISO-8859-1", "UTF-8//TRANSLIT", $row->Text1);
}
Forget about inserting the data into the MYSQL database for now. I can't get the string to display properly in my PHP page. From the examples in my listing:
echo $row->Text1
is rendered by my browser as an obviously invalid character: "Lucy�s"
all of the examples following that one are rendered as blanks: "Lucys"
It looks like a character set mismatch problem to me but how can I get this data to display properly from the MS SQL database (without changing my web-page encoding)? If I can figure that out I can probably work out the storing it in the MYSQL database part.
If the strings in the source database are encoded in UTF-8, you should use utf8_decode, not utf8_encode.
But they're probably encoded in some Latin or "Western" Windows code page. So I would try iconv("CP1252", "UTF-8", $row->Text1);, for example.
Another alternative is to run a SQL query that explicitly sets a known encoding. For example, according to the Windows Collation Name (Transact-SQL) documentation, this query would use code page 1252 to encode field Text1: SELECT Text1 COLLATE SQL_Latin1_General_CP1_CI_AS FROM ....
try this command it's working for me :
$connectionInfo = array( "Database"=>"DBName", "CharacterSet" =>"UTF-8");

Is there a way to get a MySQL table defined encoding in PHP?

I see I can get a MySQL database defined encoding in PHP using the function mysql_client_encoding(...), but is there a way to get a MySQL table defined encoding in PHP?
I see no easy way to retrieve this info. The best I could do is to do a "SHOW CREATE TABLE ;" and parse the answer:
<?php
$link = mysql_connect('localhost', 'account', 'passwd');
mysql_select_db('my_base');
$q = mysql_query('show create table my_table;');
$row = mysql_fetch_assoc($q);
preg_match("/CHARSET=(.*)/", $row['Create Table'], $matched);
echo "Table was created with charset " . $matched[1];
Which gives me:
Table was created with charset utf8
Note that charset may not be present if your table was not created with this info.
I am using SHOW CREATE TABLE query, but never from PHP.
Because I can't imagine if I ever need to run it from PHP. SET NAMES can serve any possible case.
Note that mysql_client_encoding() returns not database encoding. It's client encoding, as it says
As far as I know the encoding is defined on a per database basis in MySQL and not per table.

Categories