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.
Related
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')
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
I am trying to convert a string from HTML-ENTITIES to UTF-8 and then save the encoded string in my database. The html entities are greek letters and look for example like this: νω
Now I tried thousands of different ways, starting from just using utf8_encode or html_entity_decode until now I came across the function mb_convert_encoding().
Now the really weird thing is that when converting my string and then outputting it, it is correctly encoded to utf-8, but when inserting this string into my database I end up getting something like: ξÏνω.
This is the code for the encoding:
header('Content-Type: text/html; charset=utf-8');
mb_internal_encoding('utf-8');
......
while($arr = $select->fetch_array(MYSQLI_ASSOC))
{
$text = $arr["greek"];
$result = mb_convert_encoding($text, 'UTF-8', 'HTML-ENTITIES');
$mysqli->query("UPDATE some SET greek = '".$result."'");
}
When outputting my query and then manually doing a sql query in phpmyadmin it works fine, so it doesnt seem to be a problem of my db. There must be some problem when transferring the encoded string to my database...
As you see in your script, you are instructing the browser to use UTF8. That is the first step.
However your database needs the same thing and also the encoding/collation on the tables need to be UTF8 too.
You can either recreate your tables using utf8_general_ci or utf8_unicode_ci as the collation, or convert the existing tables (see here)
You need to also make sure that your database connection i.e. php code to mysql is using UTF8. If you are using PDO there are plenty of articles that show how to do that. The simplest way is to do:
$mysqli->query('SET NAMES utf8');
NOTE The change you will make now is final. If you change the connection encoding to your database, you could affect existing data.
EDIT You can do the following to set the connection
$mysqli = new mysqli($host, $user, $pass, $db);
if (!$mysqli->set_charset("utf8")) {
die("Error loading character set utf8: %s\n", $mysqli->error);
}
$mysqli->close();
Links of interest:
Whether to use "SET NAMES"
Execute the SET NAMES 'utf8' query prior to any others.
I have a mysql database (Wamp) which uses latin1_swedish to encode characters (é, è, ...) but for an unknown reason when I display some results from the database (using an HTML page and a PHP page) the diacritics (à, é, è) are not displayed correctly.
I've already tried to change the charset of my webpage (iso 88599-1, utf-8) but it doesn't work.
echo htmlentities($variable_name);
Try to check the encoding on your files (you can do this with notapad++), in your database (you can see this in phpmyadmin), and your database connection (you can check this in your php file that connects to the db).
They should all match
If you know the character encoding that comes in and you know what goes out, you may be able to use iconv: http://www.php.net/manual/en/function.iconv.php
For example if the db is iso-8859 and you need utf-8, you call
iconv("ISO-8859-1", "UTF-8", $text);
You can't just change the character set of a table once values are stored, because the data will not be converted. You will have to convert the data using the available MySQL functions. Read more about that at http://dev.mysql.com/doc/refman/5.0/en/charset-convert.html.
Did you set your connection to mysql db with the
SET NAMES utf8;
command?
There are several ways:
mysql_query("SET NAMES 'utf8'");
and
mysql_query("SET CHARACTER SET utf8 ");
Or, the "best";
$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);
I have a database where the table is encoded as utf8. I have a value in it that's in Korean. The characters display fine in the database. But when they are echoed from the database I get a bunch of question marks.
Here is my code, after the connect and select_db statements:
mysql_query('SET NAMES utf8');
$query = 'SELECT * FROM english WHERE id = ' . $_GET['dealerID'];
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
What am I doing wrong here? (Yes, 'english' is the right table). I tried Michael's suggestion below to encode the table as utf8_general_ci and I get a MySQL error. Suggestions? What's the correct name of the character set?
If I run a query in PHPMyAdmin, I get 서울시 서초구 서초1동 1425-10 세중프라자 4층/
Review this guide which talks about UTF8, mysql, and PHP all together.
Summary:
make sure the browser knows the page is in utf8
<?php header("Content-type: text/html; charset=utf-8"); ?>
The table in mysql needs to be set for utf8 as well as the string fields within the db
utf8_general_ci
tell php that when it talks to mysql to talk in utf8
mysqli_set_charset('utf8');
Here are some more details as well.