can't get UTF-8 names into a mysql database - php

I'm having a problem getting UTF-8 names written into a MySQL database... Here's what I have.
PHP page head has....
<meta charset="utf-8">
the MySQL column is: Char (80) with utf8_unicode_ci (these were originally latin1... I've changed them to UTF-8, truncated the database, then rerun the code)
The variable echoes to screen: Germán Mera
but writes it to database as Germán Mera
I tried putting utf8_encode(); around the variable, but then it writes to database as: Germán Mera and screen as Germán Mera (I know that command only works on iso-8859-1.. I think the JSON page is already UTF-8)
Here is an excerpt of the code I am using to get the name (for sake of simplicity, I'm only showing relevant code - I know what's shown below is not secure)
$str = file_get_contents('http://fantasy.mlssoccer.com/web/api/elements/647/');
$jsonarray = json_decode($str, true);
$name = $jsonarray['web_name'];
mysqli_query ($con, "INSERT INTO mlsprices (name) VALUES ('$name')");
Any idea how I can get this to write to the database properly? When I search, I only get quite complicated answers (eg, this) and there's surely an easier way.

Try using SET NAMES 'UTF8' after connecting to MySQL:
$con=mysqli_connect("host", "user", "pw", "db");
if (!$con)
{
die('Failed to connect to mySQL: ' .mysqli_connect_errno());
}
/* change character set to utf8 */
if (!$con->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $con->error);
}
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

Insert special characters in database from php

I have a joomla database, where the mySQL connection collation is utf8_general_ci.
I have some additional datatables in the database (not related to the joomla installation) that I want to populate with data from a PHP script.
LATER EDIT: check below
When I try inserting special characters (language and region specific characters) I get jibberish in the database, like îăîșț instead of îăîșț.
The collation for all the columns in the joomla database is : utf8mb4_unicode_ci (if this makes any difference)
The weird thing is, that if I show the content of the database in an email / mobile app (browser based) it shows the data correctly. But I can see, that something is not right with inserting the valus from PHP, since if I insert it manually from the phpmyadmin panel, the value will be correctly displayed in the table.
<?php header('Content-type: text/html; charset=UTF-8'); //header is specified here
$value_to_insert = addslashes($_REQUEST['value']); //get the value from the parameter
inserttask($value_to_insert); //insert the value into the datatable
function inserttask($value_to_insert)
{
$con = mysqli_connect("host",username,password,database); //set up my mysqli connection
//mysql_set_charset('utf8'); //this didn't help
if (!$con)
{
die();
}
$sql = "INSERT INTO `table` (`value`) VALUES ('".mysqli_real_escape_string($con, $value_to_insert)."')";
if($result=mysqli_query($con,$sql))
{
print "OK";
}
else
{
print "ERROR";
}
}
?>
Any ideas how this insert should be made, to make it compatible with any region dependend character?
Later edit:
I ran the "show variables like '%collation%'"
collation_connection - utf8_general_ci
collation_database - latin1_swedish_ci
collation_server - latin1_swedish_ci
Could this be the problem?
After setting up the connection as I had to change the connections charset in a specific way.
$con = mysqli_connect("mydb12.surf-town.net","bosteen_licadm","Ugymate92","bosteen_p2p");
$con->set_charset("utf8");
After this, the insert works!

MySQL query returning non UTF8 characters

I've got a login screen that checks entered username and password against a MySQL database.
My problem is that it doesn't recognize Swedish characters like "ÅÄÖ".
For example, the password "lösenord" is in the database but it isn't accepted, however "losenord" is.
The database has "utf8_general_ci" connection collation and I've set the charset to UTF-8 in my index.html but not in my php scripts.
I've read what feels like a million different ways to solve UTF 8 issues like this but I can't get it to work.
If someone could at least point me in the right direction I would be very thankful.
Do I need to encode each mysql query, set some META tag?
Cheers
Try using SET NAMES 'UTF8' after connecting to MySQL:
$con=mysqli_connect("host", "user", "pw", "db");
if (!$con)
{
die('Failed to connect to mySQL: ' .mysqli_connect_errno());
}
/* change character set to utf8 */
if (!$con->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $con->error);
}
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.
Also use utf8_swedish_ci in your table, otherwise string comparison will go wrong and MySQL will treat 'ö' and 'o' as the same character.

UTF-8 and German characters?

I have problem with German characters on my web site,
in html/php part of website i have this code to set utf-8:
<meta charset="utf-8">
in mysql, i have this code to set utf-8
SET CHARSET 'utf8';
Here is some word on German: Gemäß
Here is how that word looks in mysql table:
Gemäß
Here is how that word is shown on the site: Gemäß
What is a problem? Thanks.
I was using this code to get title:
$title = mysql_real_escape_string(htmlentities($_POST['title']));
I just override that to
$title = $_POST['title'];
At first, make sure, that you have UTF-8 characters in your database.
After that, try using SET NAMES 'UTF8' after connecting to MySQL:
$con=mysqli_connect("host", "user", "pw", "db");
if (!$con)
{
die('Failed to connect to mySQL: ' .mysqli_connect_errno());
}
mysqli_query($con, "SET NAMES 'UTF8'") or die("ERROR: ". mysqli_error($con));
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.
Try SET NAMES 'utf8' or SET NAMES 'utf-8'. Some of these works fine for portuguese, probably for german too. I just can't remember which one is correct, but if it is not, an error will be produced.
you should make sure that the CONNECTION is also utf-8.
with mysqli this is done with something like this:
$connection = mysqli_connect($host, $user, $pass, $db_name);
$connection->set_charset("utf8");
Now if somehow you ended up with wrong characters in the database there is a way to make it right:
in a PHP script, retrieve the information as you do now, i.e without setting the connection. This way the mistake will be inverted and corrected and in your php file you will have the characters in the correct utf-8 format.
in a PHP script, write back the information with setting the connection to utf-8
at this point you should see the character correct in your database
now change all your read/write functions of your site to use the utf-8 from now on
in HTML5 use
<meta charset="utf-8">
in HTML 4.0.1 use
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
the results are html entity encoded as if they were processed by htmlentities(), I wonder if your variables are ibserted as received from the form or are being processed by say a wysiwg editor for instance?
Anyway, these should print fine on an html template but an html_entity_decode() should do it to.
Hope this helps
Set the data type in your database to use UTF-8 as well, this should solve the problem.
I had the same problem. which I solved by using:
if you have already created your table, you need the modify the character set as:
alter table <table name> convert to character set utf8 collate utf8_general_ci.
your tables character set is set to latin_swedish by default by MySQL.
also, you might face some problems while retrieving the data and displaying it to you page.For that include: mysql_set_charset('utf8') just below the line where you have connected your database.
eg:
mysql_connect('localhost','root','');
mysql_select_db('my db');
mysql_set_charset('utf8');
You will need to do this for php 5.x
$yourNiceLookingString =
htmlspecialchars ($YourStringFromDB, ENT_COMPAT | ENT_HTML401, 'ISO-8859-1');
and for php 4.x
$yourNiceLookingString = htmlspecialchars($YourStringFromDB);

Converting html entities to utf-8 and inserting them into a mysql database

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.

Issue when inserting UTF8 characters in db from script

I have a small script that receive some data through GET and inserts data in a db. My problem is when sending some UTF-8 characters. The scritp receives them ok but inserts them in a weird way. I printed the query in my page, executed it with phpmyadmin and works ok that way. So, my problem is when executing the query through my script (it doesn't work if I execute a constant query with those characters). Does sending characters by post resolve the issue?
Thank you
Try this:
mysql_set_charset('utf8');
Your entire setup has to be UTF-8. That means your web page, PHP, the database connection, AND the database tables, all have to be in UTF-8 mode. Given it works in the admin pages and not via script, I'm guessing it's your database connection. Try doing a set names='utf-8' before doing your insert query, and see if that fixes things. If it does, then your db connection is using some OTHER character set and mangling your text as it goes from PHP->database.
Add this in the php file before your query:
mysql_query('SET NAMES utf8');
For example:
//DB connection
$con = mysql_connect('localhost', 'root', '');//db details
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydatabase", $con);//DB name
mysql_query('SET NAMES utf8');
//query
$sql="SELECT * FROM users WHERE uid=".$user_id;// your query
$result=mysql_query($sql);

Categories