This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 3 years ago.
im not new in php coding and have a problem. I need insert data to sql (czech language encoding).
I wanna input this string: +ěščřžýáíéůú
My DB output is: +ì¹èø¾ýáíéùú
So, im trying this solutions:
1) My config.php has this one:
header('Content-Type: text/html; charset=utf-8');
ini_set("default_charset", 'utf-8');
2) On top of the page is this meta-tags:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta charset="utf-8" />
3) When im connecting into db, im using:
#define("DB_Connect_Charset", "utf8");
$dsn = 'mysql:dbname='.DB_Connect_Database.';host='.DB_Connect_Hostname.';charset='.DB_Connect_Charset;
+
$this->pdo = new PDO($dsn, DB_Connect_Username, DB_Connect_Password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES ".DB_Connect_Charset));
Im trying some php functions like: iconv OR mb_convert_encode()
Database has utf8_czech_ci coding, when im trying to change it into windows-1251 or latin.. it doesnt help.
I spend a lot of time on stackoverflow and cannot find a solution about this problem, some problems will be deleted with SET NAMES UTF8.
Problem must be in part when im sending data to SQL, because if im put data manually into sql, im fetching it correctly.
Thank you for your time and help. Have a nice day!
When I'm binding parameters I use utf8_encode function for value. It was a mistake.
Related
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed last year.
how can I make my PHP script echo values from MySQL with proper encoding?
Currently, it returns as shown in a screenshot below while the database is set to coding also shown below.
If you are using PDO, please specify the charset when you create the db connection as follows:
$conn = new PDO("mysql:host=$host;dbname=$db;charset=utf8_czech_ci", $user, $pass);
Whereas if you are using mysqli, please use the following :
$conn = mysqli_connect($servername, $username, $password, $dbname);
/* change character set to utf8_czech_ci */
mysqli_set_charset($conn,"utf8_czech_ci");
$mysqli->query("set names utf8_czech_ci");
There’s a few issues that can be causing this (went through the same bs myself)
Above works if it's just your database in the wrong default charset, you also have to set both PHP and HTML headers to the correct encoding.
header('Content-Type: text/html; charset=utf-8');
ini_set("default_charset", "UTF-8");
mb_internal_encoding("UTF-8");
iconv_set_encoding("internal_encoding", "UTF-8");
iconv_set_encoding("output_encoding", "UTF-8");
Can all affect it. Looks like the data is saving correctly, so it's just a presentation issue. Without more info it's hard to say if it's a php, html, or library issue but the above should pretty much nuke the issue.
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 7 years ago.
I am using tamil language (utf-8) inserted into MySQL database collation latin1_swedish_ci by default. but the data shows like ????? ??? ???????????.????? when I retrieve it. I studied in the net lot about the problem. but the solution nothing was useful. Totally it is making mad. anybody can help me. I am using query below like this. Give me solution in simple way only.
<?php
$con=mysql_connect('localhost','root','');
$db=mysql_select_db('nikah', $con);
$sql="select * from matrimony";
$result=mysql_query($sql) or die(mysql_error());
While($row=mysql_fetch_array($result)){
echo $row['name'];
}
?>
Try using mysql_set_charset() as explained here.
$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);
EDIT:
As stated by Jay keep in mind that mysql extension has been deprecated in PHP 5.5.0. From the php doc:
Warning
This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
mysqli_set_charset()
PDO: Add charset to the connection string, such as charset=utf8
In addition to setting mysql_set_charset("utf8"); as mentioned in the other answer, you might need to adjust for a few more settings in order to fully guard yourself against broken characters.
Connection
The connection needs to know what charset to expect. Just after creating the connection, specify the charset like this
$con = mysql_connect('localhost','root','');
mysql_set_charset("utf8");
Headers
Setting the charset in both HTML and PHP headers to UTF-8
PHP: header('Content-Type: text/html; charset=utf-8');
(PHP headers has to be placed before any kind output (echo, whitespace, HTML))
HTML: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
(HTML-headers are placed within the <head> / </head> tag)
Database and tables
Your database and all its tables has to be set to UTF-8. Note that charset is not exactly the same as collation (see this post).
You can do that by running the queries below once for each database and tables (for example in phpMyAdmin)
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Should you follow all of the pointers above, chances are your problem will be solved. If not, you can take a look at this StackOverflow post: UTF-8 all the way through.
mysql_* functions are deprecated since PHP 5.5 (and removed entirely in PHP 7) and you shoud stop using them if you can.
You should choose another API, like mysqli_* or PDO instead - see choosing an API.
I am trying to print data from MySql database.
I have a simple problem it is showing D�lar instead of Dólar .
Although I have included
<META http-equiv="Content-Type" Content="text/html; charset=utf-8">
In my html page so can any one help me out with this
Thanks in Advance
The character set needs to be defined in a few different places:
The MySQL database
The text stored in the database might not be encoded as UTF-8. You can define a default character set as part of the create database statement:
CREATE DATABASE mydb CHARACTER SET utf8;
You can also specify per-column character sets with create table.
Within your PHP code
You'll need to tell your client-side code which encoding it should use when communicating with the database.
If you're using PDO, you can specify the character set as part of the DSN string:
$dsn = 'mysql:host=localhost;dbname=testdb;charset=utf8';
$dbh = new PDO($dsn, $username, $password);
If you're using MySQLi, you can use the mysqli_set_charset() function/method:
$dbh->set_charset('utf8');
or:
mysqli_set_charset($dbh, 'utf8');
Alternatively, the MySQL website suggests issuing a statement after connecting to the server:
SET NAMES 'utf8';
Within the HTML output
For HTML5, you can simply add the following <meta> tag within the <head> element of your output:
<meta charset="utf-8">
I've used bellow function and its working for me
call_user_func_array('mb_convert_encoding', array(&$str,'HTML-ENTITIES','UTF-8'));
Firstly, I know this question has been asked a lot of times but all the answers I got, they say that I have to put all my encoding into UTF-8, but actually, they're all with UTF-8 and still not working!! Here is my problem:
I have a registration in my webpage, and when a user with a ñ in his name registers, it stores it in the database as ñ, so if I register with the name "ñaña", it goes to the db as "ñaña".
I have set my db table and database to utf8_general_ci, and I also have this code in my header:
<meta charset="utf-8">
<meta http-equiv="Content-type" content="text/html;charset=utf-8" />
And this code in my PDO connection:
$connection = new PDO("mysql:charset=utf8;host=$host; dbname=$dbname", $user, $password);
But it's still stored in the db as a different character... Also, all the accents like "à, é, ê, ö..." are working fine.
What am I missing?
Thanks.
You may have created your tables with a character encoding that doesn't support special. characters; that would explain what you're seeing. You can try these SQL commands to discover the charset of your database/table/column.
If your tables are encoded with something other than utf8, you can use these other SQL commands to convert them to utf8. But I'm not sure what will happen with the current data.
You can create a function to replace the values
function sanitize($string)
{
$string = str_replace('ñ', 'ñ', $string);
return $string;
}
Hope it works for you
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
UTF-8 all the way through
I'm developing some new features on a website that somebody else already developed.
I'm having a problem the charset.
I saw that the database had some tables in utf8 and some in latin1
So I'm trying to convert all the tables in UTF8.
I did it for one table (also the fields of this table now are utf8), but was not successful.
I'm using the normal mysql connect. I have to put any config to say that it must connect with utf8 to the DB? If yes witch one?
In my html I have:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
It looks like some letters works and others display the question mark.
For example it not able to display this ’ that is different of this: '
Try this
<?php
header('Content-Type: text/html; charset=utf-8');
?>
and then in the connection
<?php
$dbLink = mysql_connect($argHost, $argUsername, $argPassword);
mysql_query("SET character_set_results=utf8", $dbLink);
mb_language('uni');
mb_internal_encoding('UTF-8');
mysql_select_db($argDB, $dbLink);
mysql_query("set names 'utf8'",$dbLink);
?>