mysql_query("SET NAMES 'utf8'");
mysql_query('SET CHARACTER SET utf8');
$sql = "SELECT * FROM mytable";
$data = selectArray($sql);
insert arabic data is = `تظهر البيانات العربي
display in my database = تظهر البيانات العربيØ
display in cakePhp = تظهر البيانات العربية (display proper)
display in corePhp = تظهر البيانات العربيØ
Let me know any one have solution of this issue.
when create table use like this
CREATE DATABASE mydb DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
Mojibake -- search this forum for that. You hit a common problem; this is a duplicate.
Related
This question already has answers here:
strange character encoding of stored data , old script is showing them fine new one doesn't
(2 answers)
How to convert an entire MySQL database characterset and collation to UTF-8?
(20 answers)
Closed 7 years ago.
I have Arabic text stored in mysql like this format
شقق جديده غبر مسكونه 220 متر..
.150متر..طبربور ..ااسكانات قموم
والنجار للاستÙسار
it is in this format because I didn't use the below before inserting data into database from HTML textboxes:
mysql_query('SET CHARACTER SET utf8');
My question now, how to convert the text into MySQL columns to readable text like this:
شقق جديده غبر مسكونه 220 متر ..150متر..طبربور ..ااسكانات قموم والنجار للاستفسار
I tried the below for one column cell to check, but it didn't work:
mysql_query('SET CHARACTER SET utf8');
$sql = 'SELECT content FROM messages WHERE id=500';
$qry = mysql_query($sql);
$result =mysql_fetch_object($qry);
$text= $result->content;
mysql_query('SET CHARACTER SET utf8');
$sql ='UPDATE messages SET content= "'.$text.'" where id=500';
mysql_query($sql);
Note: if I use the below
mysql_query('SET CHARACTER SET utf8');
before I insert data into database from HTML textarea, then it works fine for store and read, but I need to convert the already entered data into tables.
Thanks.
The solution in brief can be done in 3 simple steps:
A. Change the Database collation to utf8 via the command below:
ALTER DATABASE <db_name> CHARACTER SET utf8 COLLATE utf8_general_ci;
already the table cells I want to change have collation of utf8_general_ci
B. Implement this query to change the content:
update messages set content=CONVERT(BINARY CONVERT(content USING latin1) USING utf8);
C. add the below before you connect to the DB:
mysql_query('SET CHARACTER SET utf8');
And you are done!!! Thanks for all
Use the ALTER DATABASE and ALTER TABLE commands.
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Source.
i have an mysql table member.
member name has arabic names. mname collation set as utf8_general_ci.
when user enters arabic name its stores in the db table as some other characters. but when i retrieve and display it in the site, it displays correctly as Arabic text. why its not storing as arabic text in the database table ?
i tried this but only i am getting question marks no Arabic text...
1- MySQL charset: UTF-8 Unicode (utf8)
2- MySQL connection collation: utf8_general_ci
3- your database and table collations are set to: utf8_general_ci or utf8_unicode_ci
Then, add this code in your php script when you connect to db:
mysql_query("SET NAMES 'utf8'"); mysql_query('SET CHARACTER SET utf8');
Php Code:
using simple php.
$mname = $_POST['mname'];
$email = $_POST['email'];
$password = $_POST['password'];
$gender = $_POST['gender'];
$age = $_POST['age'];
$city = $_POST['city'];
$country = $_POST['country'];
$query="insert into member (mname, email, password, gender, age, city, country) values ('$mname','$email','$password', '$gender', '$age','$city','$country')";
mysql_query("SET NAMES 'utf8'");
mysql_query('SET CHARACTER SET utf8');
mysql_query($query);
that could be problem with that to set meta tag
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
also you have to set column collection type it should be same utf8_general_ci. more over table and database type will also consider as same.
And also some point to be carefull like
o read ,write and sort Arabic text in mysql database using php correctly, make sure that:
1- MySQL charset: UTF-8 Unicode (utf8)
2- MySQL connection collation: utf8_general_ci
3- your database and table collations are set to: utf8_general_ci or utf8_unicode_ci
Then, add this code in your php script when you connect to db:
mysql_query("SET NAMES 'utf8'"); mysql_query('SET CHARACTER SET utf8');
for more details: LINK
let me know if i can help you more.
I can't insert Arabic language to Mysql database using the below code. When I check the values using phpMyAdmin, the field would be empty. Why is that so?
$query = "INSERT INTO table (name) VALUES ('عماد')";
mysql_query($query);
name field should be utf8
in the same time use this query directly after connecting to mysql, before executing any other query like the insert statement in your example
set names 'utf8'
then check phpmyadmin output, it should be correct
Use this line .... u can insert special charecters.
mysql_real_escape_string($query);
/**
* ALTER TABLE `myTable` CHARACTER SET utf8 COLLATE utf8_general_ci;
*
* There’s a generic way of doing by simply executing the MySQL query:
* SET NAMES utf8;
*
* ...and depending on which client/driver you’re using
* there are helper functions to do this more easily instead:
*/
// With the built in mysql functions
mysql_set_charset('utf8');
// With MySQLi
$mysqli->set_charset("utf8")
// With PDO_MySQL (as you connect)
$pdo = new PDO(
'mysql:host=localhost;dbname=test',
'username',
'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
Make sure the field's name collation is set to: 'utf8_unicode_ci' in PHPMyAdmin
Goto you database and change your field's Collation value to utf8_unicode_ci .Thanks me later
for Java tuts visit: Introduction to Java
I have a question about converting charset from inside mysql query.
I have a 2 databases. One for the website (joomla), the other for forum (IPB).
I am doing query from inside joomla, which by default have "SET NAMES UTF8".
I want to query a table inside the forum databases. A table called "ibf_topics". This table has latin1 encoding.
I do the following to select anything from the not-utf8 table.
//convert connection to handle latin1.
$query = "SET NAMES latin1";
$db->setQuery($query);
$db->query();
$query = "select id, title from other_database.ibf_topics";
$db->setQuery($query);
$db->query();
//read result into an array.
//return connection to handle UTF8.
$query = "SET NAMES UTF8";
$db->setQuery($query);
$db->query();
After that, when I want to use the selected tile, I use the following:
echo iconv("CP1256", "UTF-8", $topic['title'])
The question is, is there anyway to avoid all this hassle?
For now, I can't change forum database to UTF8 and I can't change joomla database to latin1 :S
you could open 2 database connections, 1 to joomla and one to IPB. you'll be using more resources, but it will make your code easier to read, and you can do all the configuration (charset etc) in the db setup. this will also make it easier to migrate later, in case your situation changes - you won't need to go through your code removing all the SET NAMES.
$query = "SET NAMES UTF8";
$db->setQuery($query);
$db->query();
$query = "select id, title from other_database.ibf_topics";
$db->setQuery($query);
$db->query();
/*
you can set charset after database connection
*/
header('Content-Type: text/html; charset=utf-8');
/*
after connection
*/
mysql_query("SET NAMES utf8");
/*
or
*/
mysql_query("SET CHARACTER SET utf8");
/*
or
*/
mysql_query("SET COLLATION_CONNECTION = 'utf8_general_ci'");
I have to store hindi text in a MySQL database, fetch it using a PHP script and display it on a webpage. I did the following:
I created a database and set its encoding to UTF-8 and also the collation to utf8_bin.
I added a varchar field in the table and set it to accept UTF-8 text in the charset property.
Then I set about adding data to it. Here I had to copy data from an existing site.
The hindi text looks like this: सूर्योदय:05:30
I directly copied this text into my database and used the PHP code echo(utf8_encode($string)) to display the data. Upon doing so the browser showed me "??????".
When I inserted the UTF equivalent of the text by going to "view source" in the browser, however, सूर्योदय translates into सूर्योदय.
If I enter and store सूर्योदय in the database, it converts perfectly.
So what I want to know is how I can directly store सूर्योदय into my database and fetch it and display it in my webpage using PHP.
Also, can anyone help me understand if there's a script which when I type in सूर्योदय, gives me सूर्योदय?
Solution Found
I wrote the following sample script which worked for me. Hope it helps someone else too
<html>
<head>
<title>Hindi</title></head>
<body>
<?php
include("connection.php"); //simple connection setting
$result = mysql_query("SET NAMES utf8"); //the main trick
$cmd = "select * from hindi";
$result = mysql_query($cmd);
while ($myrow = mysql_fetch_row($result))
{
echo ($myrow[0]);
}
?>
</body>
</html>
The dump for my database storing hindi utf strings is
CREATE TABLE `hindi` (
`data` varchar(1000) character set utf8 collate utf8_bin default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `hindi` VALUES ('सूर्योदय');
Now my question is, how did it work without specifying "META" or header info?
Thanks!
Did you set proper charset in the HTML Head section?
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
or you can set content type in your php script using -
header( 'Content-Type: text/html; charset=utf-8' );
There are already some discussions here on StackOverflow - please have a look
How to make MySQL handle UTF-8 properly
setting utf8 with mysql through php
PHP/MySQL with encoding problems
So what i want to know is how can i
directly store सूर्योदय into my
database and fetch it and display in
my webpage using PHP.
I am not sure what you mean by "directly storing in the database" .. did you mean entering data using PhpMyAdmin or any other similar tool? If yes, I have tried using PhpMyAdmin to input unicode data, so it has worked fine for me - You could try inputting data using phpmyadmin and retrieve it using a php script to confirm. If you need to submit data via a Php script just set the NAMES and CHARACTER SET when you create mysql connection, before execute insert queries, and when you select data. Have a look at the above posts to find the syntax. Hope it helps.
** UPDATE **
Just fixed some typos etc
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_query('SET character_set_results=utf8');
mysql_query('SET names=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_results=utf8');
mysql_query('SET collation_connection=utf8_general_ci');
mysql_select_db('onlinetest',$con);
$nith = "CREATE TABLE IF NOT EXISTS `TAMIL` (
`data` varchar(1000) character set utf8 collate utf8_bin default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1";
if (!mysql_query($nith,$con))
{
die('Error: ' . mysql_error());
}
$nithi = "INSERT INTO `TAMIL` VALUES ('இந்தியா நாட்டின் பக்கங்கள்')";
if (!mysql_query($nithi,$con))
{
die('Error: ' . mysql_error());
}
$result = mysql_query("SET NAMES utf8");//the main trick
$cmd = "select * from TAMIL";
$result = mysql_query($cmd);
while($myrow = mysql_fetch_row($result))
{
echo ($myrow[0]);
}
?>
</body>
</html>
For those who are looking for PHP ( >5.3.5 ) PDO statement, we can set charset as per below:
$dbh = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
CREATE DATABASE hindi_test
CHARACTER SET utf8
COLLATE utf8_unicode_ci;
USE hindi_test;
CREATE TABLE `hindi` (`data` varchar(200) COLLATE utf8_unicode_ci NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `hindi` (`data`) VALUES('कंप्यूटर');
For Those who are facing difficulty just got to php admin and change collation to utf8_general_ci
Select Table go to Operations>> table options>> collations should be there