how to correct font error of mysql - php

hello i am using MYSQL for my data base. When i use Mongolian font. it saves in MYSQL like ????. How to correct this?
When i save in English it looks ok but in Mongolian it looks like ??? in the picture.
In the screen it also looks like ????
my MYSQL insert code is:
$sql ="INSERT INTO core_network (system_name,sub_cat,location,alarmtype,severity,start_time,end_time,reason,shift_operation)
VALUES ('$S_name','$subcategory','$city','$a_type','$severity','$S_time','$F_time','$reason','$shift_operation')";
how to correct?

$connection = mysql_connect($host1, $user, $pass)
or die("Could not connect: ".mysql_error());
mysql_select_db($database1,$connection)
or die("Error in selecting the database:".mysql_error());
after connect to DB and before use "SELECT" use this code :
mysql_query("SET CHARACTER SET utf8",$connection);

You need to set the collation of the columns to an UTF-8 one like utf8_general_ci.
Also you have to make sure that the connection charset is UTF-8 as well, for example by issuing
SET NAMES "utf8"
before any other SQL.

Related

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

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.

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

– like symbols when using text editor

When I used text editors to insert text into database, symbols like - , ' etc get converted to – like symbols. How can I avoid these symbols?
Set names to UTF8 before inserting into your database.
SET NAMES utf8
It sounds like your connection is using latin1 encoding. I struggled with the same thing for months.
http://forums.mysql.com/read.php?103,46870,47245
You need to set encoding to utf8 using method mysql_set_charset('utf8',$link);
<?php
$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);
$db_selected = mysql_select_db('your_db_name', $link);
?>
And then the usual stuff you do with your connection
Check the encoding (collation) used on the database. Try changing it to utf8_general_ci or the equivalent choice on the Database you are using.

ÆØÅ doesn't display correctly in MySQL

I have set up a table in phpMyAdmin. I haven't changed the charsets or anything. I inserted a text in a new row, and when I try to SELECT that row and output it with PHP, the letters ÆØÅ are displayed as ���, however if I try to edit the field in phpMyAdmin, the letters are displayed correctly. What do I do wrong that phpMyAdmin does correctly?
If your PHP file is already UTF-8 encoded, you should tell your database, that you need UTF-8. Instead of fiddling with the configurations of MySQL, just tell your connection object, which character-set you expect, the database does the rest for you.
This is an example for a mysqli connection object:
$db = new mysqli($dbHost, $dbUser, $dbPw, $dbName);
$db->set_charset("utf8");
Afterwards your queries will return UTF-8 encoded results.
SET NAMES 'charset_name'
http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
First you will want to determine if it is the browser charset that is wrong, or mysql.
Try swapping the charset in your browser to utf8 or if it is already to iso-8859.
If that doesn't fix it try changing the charset in your query by doing
SET CHARACTER SET charsetname;

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