I am using PHP and MySQL for an application.
Questions are:
How to store data in MySQL नवीन खेतिहर उपकरण। readable format or निलेस र पà¥à¤¬ format
When user enters data in a textbox and clicks on submit, at that time we get data in different format. What we need to do should we convert and store in MySQL in readable format.
Choose utf8 character set and utf8_general_ci collation.
Obviously, Collation of the field (to which you want to store Hindi text) should be utf8_general_ci.
To alter your table field, run
ALTER TABLE `<table_name>` CHANGE `<field_name>` `<field_name>` VARCHAR(100)
CHARSET utf8 COLLATE utf8_general_ci DEFAULT '' NOT NULL;
Once you've connected to database, run the following statement at first
mysql_set_charset('utf8');
Eg:
//setting character set
mysql_set_charset('utf8');
//insert Hindi text
mysql_query("INSERT INTO ....");
To retrieve data
//setting character set
mysql_set_charset('utf8');
//select Hindi text
mysql_query("SELECT * FROM ....");
Before you printing any unicode text (say Hindi text) on browser, you should have to set content type of that page by adding a meta tag
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Eg:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example Unicode</title>
</head>
<body>
<?php echo $hindiText; ?>
</body>
</html>
Update:
mysql_query("SET CHARACTER SET utf8") has changed tomysql_set_charset('utf8');
This is the preferred way to change the charset. Using mysql_query() to set it (such as SET NAMES utf8) is not recommended. See http://php.net/manual/en/function.mysql-set-charset.php*
You only need to run the following command on the table.
ALTER TABLE <tablename> CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Note: You should use utf8_general_ci instead of ut8_unicode_ci, as it is said to be faster. What's the difference between utf8_general_ci and utf8_unicode_ci
There is no need to alter table with charset. but your database should be with utf8_unicode_ci.
while connecting with database just need to add this line only." mysqli_set_charset($con, 'utf8')"
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
mysqli_set_charset($con, 'utf8');
mysqli_query($con, $sql);
After converting COLLATE to utf8_general_ci use this line
mysqli_set_charset($con, 'utf8'); in between $con and mysqli_query() below your query.
$con= mysqli_connect($host,$user,$pass,$db) or die('Unable to connect');
$sql="your query.. ";
mysqli_set_charset($con, 'utf8');
$result=mysqli_query($con,$sql);
Related
I included this at the top of my php file:
<?php
header('Content-Type: text/html; charset=UTF-8');
?>
I did this because my file.php was not displaying "á, é, í, ó, ú or ¿" in the html file or from data queried from my database.
After I placed the 'header('Content-Type: text/html; charset=UTF-8');' line of code my html page started to understand the special characters in the html file but, data received from my database now has a black rhombus with a question mark.
The collation my database has is "utf8_spanish_ci"
at the html tag i tried to put lang=es but this never worked I also tried to put the meta tag inside the head tag
<!DOCTYPE html>
<html lang=es>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<head>
I also tried:
<meta charset="utf-8">
and:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
I don't know what the problem is. When I insert data directly into the data base the special characters are there but when I insert them from my file.php they appear with random characters.
Does anyone know why this is happening?
There are a couple of reasons this could be happening. It is however important that your entire line of code uses the same set of charset, and that all functions that can be set to a specific charset, is set to the same. The most widely used one is UTF-8, which is the one I'm suggesting you use.
Connection
You also need to specify the charset in the connection itself.
PDO (specified in the object itself):
$handler = new PDO('mysql:host=localhost;dbname=database;charset=utf8', 'username', 'password', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET UTF8"));
MySQLi: (placed directly after creating the connection)
* For OOP: $mysqli->set_charset("utf8");
* For procedural: mysqli_set_charset($mysqli, "utf8");
(where $mysqli is the MySQLi connection)
MySQL (depricated, you should convert to PDO or MySQLi): (placed directly after creating the connection)
mysql_set_charset("utf8");
Database
Your database and all its tables has to be set to UTF-8. Note that charset is not the same as collation.
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;
File-encoding
It's also important that the .php file itself is UTF-8 encoded. If you're using Notepad++ to write your code, this can be done in the "Format" drop-down on the taskbar (Convert to UFT-8 w/o BOM). You should use UTF-8 w/o BOM.
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.
Are you sure? And are you sure that you are retrieving your data from the data base? Having said that, most databases require you to save data in a way that is NOT exactly like your question. There is really valid security reasons for this.
You should use utf8_general_ci as a database encoding also before insert query you should run this query
Mysql_query(" SET NAMES 'utf8'");
I'm trying to store an Arabic text to the table, I searched a lot but I didn't find a solution that worked for me, so this is what I got:
$en = "OK";
$ar = "حسناً";
$link->query("INSERT INTO words (en,ar) VALUES ($en,$ar)");
The problem is when I insert it, the Arabic text looks like Øسناً, my table's collation and MySQL's are utf8_general_ci, so is my database's, I also have mysql_query("SET NAMES 'utf8'"); mysql_query('SET CHARACTER SET utf8');, but it doesn't work.
I recently had the same issues myself.
Here's a few pointers:
ALL attributes must be set to ut8 (collation is NOT the same as charset)
Save the document as UTF-8 (If you're using Notepad++, it's Format -> Convert to UFT-8)
The header in both PHP and HTML should be set to UTF-8 (HTML: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> and PHP: header('Content-Type: text/html; charset=utf-8');
Upon connecting to the databse, set the charset ti UTF-8 there as well, like this: $link->set_charset("utf8"); (directly after connecting)
Also make sure your database and tables are set to UTF-8, you can do that like this:
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Remember that EVERYTHING needs to be set to UFT-8 charcode, or else it'll insert stuff like "Øسناً". Hope this helped!
First thing is the database, table, and column. See if utf8 is set:
utf8_general_ci or utf8_unicode_ci
Also, the connection collation: utf8_general_ci
In PHP, after I connect with mysqli, I issue this:
$conn->query("SET NAMES 'utf8'");
And the web page output always jams in:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Here is a solution that worked for me:
when you create the database choose the collation for: utf8_unicode_ci
when you connect to the database from php use the following character set:
$dsn = "mysql:host=localhost;dbname=record;charset=utf8";
I am trying to display chinese characters that I have saved in MySQL database, which is saved under utf8_unicode_ci type. I have seen several solutions on the web, but nothing works.
Below is my connection file:
$conn = mysql_connect("localhost","root","password");
mysql_set_charset('utf8',$conn);
mysql_query("SET CHARACTER SET utf8 ");
mysql_select_db("database");
Below is my query:
mysql_query("SET character_set_results=utf8", $conn);
$sql = mysql_query("select * from webdata",$conn);
But it still shows ????. Any ideas?
How to resolve...
When I had a similar issue I firstly displayed the encoding of my text in php using
echo mb_detect_encoding ( $text );
It shows the encoding of the text coming from my query. This showed me that I was getting ASCII from mysql_query when Chinese or Russian characters were in my database.
The change I made was with the following addition after the mysql_connect
mysql_set_charset('UTF8');
My database is utf8_unicode_ci collation and I can see chinese characters.
So, if mb_detect_encoding is now giving you UTF8 for your text then you would be able to show chinese characters.
The next step is to make sure what you pass to the browser has the correct header...
header('Content-Type: text/html; charset=UTF-8');
Put the above at the top of your code in php to make sure the browser is expecting your encoding.
Now that should the question, however ideally you should be using PDO or mysqli rather than mysql_connect method. In this case the equivalent procedural style commands are..
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'test');
mysqli_set_charset($link, "utf8");
Where $link is the equivalent to your connection to the database.
where it show "???", when you print the output to HTML ?
if so, try to add to <head> element the line
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
hope it helped a bit.
EDIT
it seems that you need to declare UTF8 on:
character_set_results = 'utf8',
character_set_client = 'utf8',
character_set_connection = 'utf8',
character_set_database = 'utf8',
character_set_server = 'utf8'"
checkout
PHP UTF8 not displaying chinese characters properly
That should be all you need. Both for Traditional and Simplified Chinese characters
1. Make sure your table is set to COLLATION utf8_general_ci
2. $con = new mysqli("localhost",$username,$password,$database) or die("Error " . mysqli_error($con)); $con->query("SET NAMES 'utf8'");
3. <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
I have a database which is encoded in UTF8_bin.
Whenever I try to echo something on that database I get questionmarks instead of letters. Anyone knows a solution for that? I think it is important to mention that if I do echo to a word in UTF8 it is just fine. The problem is getting the data from the database.
Please check whether you have followed these steps.
-> db collation must be utf8_general_ci
-> then collation of the table with language has to be utf8_general_ci
-> in your php connection script put header('Content-Type: text/html; charset=utf-8');
-> in xhtml head tag put <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
-> after selecting the db in the connection script put mysql_query("SET NAMES 'utf8'");
Then check the connection like this,
if(!mysqli_set_charset($conn, 'utf8')) {
echo 'the connection is not in utf8';
exit();
}
I'm developing a website in Brazilian Portuguese and I'm facing some really annoying encoding problems.
Words that should be written this way: óbito are being written this way: �bito
I have noticed that while the texts are still at the database they are ok. But when I use echo with PHP, the encoding problem comes up.
List of things I have already done and did not help:
1- Set the PHP files to be saved as UTF-8
2- I'm using this meta tag <meta http-equiv="content-type" content="text/html; charset=utf-8" />
3- I used this SQL Queries:
CREATE DATABASE db_name
CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci
;
ALTER DATABASE db_name
CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci
;
ALTER TABLE tbl_name
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
;
Dont try to reinvent the wheel, keep things simple:
just use the following line after selecting the database:
mysql_query("SET NAMES 'utf8'") OR die(mysql_error());
You can change the charset using this function:
$q = mysql_set_charset('utf8');
var_dump($q);
(if the function returns true it's been successful). This should fix your problems for the connection.
For older versions of PHP, you can use the following:
<?php
if (function_exists('mysql_set_charset') === false) {
/**
* Sets the client character set.
*
* Note: This function requires MySQL 5.0.7 or later.
*
* #see http://www.php.net/mysql-set-charset
* #param string $charset A valid character set name
* #param resource $link_identifier The MySQL connection
* #return TRUE on success or FALSE on failure
*/
function mysql_set_charset($charset, $link_identifier = null)
{
if ($link_identifier == null) {
return mysql_query('SET NAMES "'.$charset.'"');
} else {
return mysql_query('SET NAMES "'.$charset.'"', $link_identifier);
}
}
}
?>
It seems like PHP uses latin1 by default and I can't find a way to change the default. So I guess you'll have to use mysql_set_charset() every time you start a new connection.
Boa Sorte.
Mine is solved by putting:
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
in the header of the page, besides creating the tables again and changing the collation of database to utf-8 encoding.
Use this for Portuguese :
<meta http-equiv="Content-Type" content="text/html; charset=pt-BR" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Putting above meta tag inside <head> tag of your html/php document.
And you also need to run this query mysql_query("SET NAMES 'utf8'"); in your main functions file if you are using MySQL database with PHP.
Is solved by Putting: After the database connection string
$con = mysqli_connect('localhost', 'root', 'password', 'testdb');
if (mysqli_connect_errno()) {
exit("Failed to connect to MySQL: " . mysqli_connect_error());
}
mysqli_set_charset($con, 'utf8');