How to make Mysql database to support Arabic Language? - mysql

I am trying to save Arabic language in mysql database but it doesnot save in Arabic format.
It shows question marks instead of Arabic. How to make it store values in Arabic.
I tried many queries seeing from internet but it doesnot changes. How to change it for Arabic.
"ar_SA: Arabic - Saudi Arabia"
Please suggest a way?

i use WAMP Server. (windows,apache,mysql,php).
//so important
FIRST :
in phpmyadmin or MySQL :
make sure that Mysql Database is utf.
make sure that the your database and it's tables are utf-general-ci
after connecting to Mysql immidiately (before choosing your DB) make this order.
mysql_set_charset('utf8');
example :
<?php
//connect to MySQL
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_set_charset('utf8'); // that's the order.
echo "Connected to MySQL<br />";
//connect to your DB
mysql_select_db("mydb") or die(mysql_error());
echo "Connected to Database";
?>
SECOND :
in the meta data in the php file make the meta data as following :
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Third :
Make sure that the php file it self is utf-8 enabled . u can make sure in your IDE settings , or if u work on notepad++ or Komodo Edit then u can find it in the status bar at the bottom of the window , right side.
// i tried this but it didn't have effect.
in the header of the php file (before every thing).
<?php header("Content-type: text/html; charset=utf-8"); ?>
in the form submitted:
<form accept-charset="utf-8" ...>

Just use UTF-8 in the page's encoding, in the database connection, and the database itself.

Make sure your database encoding and collation is utf8_general_ci

Related

Connecting to a MySQL database using DBI

I am trying to connect to a MySQL database.
I found this script and I am trying to use it on my PC and web host, but it doesn't show any output.
Please have a look at this code. I am running perl at xampp.
#!C:\xampp\perl\bin\perl.exe
print "Content-type: text/html\n\n";
use DBI;
use strict;
my $driver = "localhost";
my $database = "data";
my $dsn = "DBI:$driver:database = $database";
my $userid = "root";
my $password = "";
my $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr;
I am using the same database with PHP.
but it doesn't show any output.
Judging by the CGI header you're displaying, I assume that you're running this as a CGI program. In which case, it's no surprised that you're not seeing any output - you're not sending any output.
If you ran it as a command line program (and it's often a good idea to get stuff working on the command line before leaping into CGI programming), then you would see the "Content-Type" header. Alternatively, you could add some output to your program and see if that appears in your browser. Something simple like:
print 'It works!';
I'd also like to add, that CGI looks rather outdated these days and there are far better (by which I mean easier and more powerful) ways to write web applications with Perl. You might like to read CGI::Alternatives to get an idea of what is available.
Update:
I've just seen this question asked on Facebook (please don't cross-post without telling people) and I've noticed that your $driver variable is wrong. If you're connecting to MySQL, then $driver should be "mysql" (so that DBI loads "DBD::mysql").
A DSN for the MySQL driver looks like this
DBI:mysql:database=$database;host=$hostname;port=$port
where the host and port fields are optional. (The database is also optional, but you don't want to leave that out.) There are several more esoteric options too, but they're irrelevant here
But you're supplying
DBI:localhost:database = data
Which doesn't even specify a MySQL connection, so I'm not surprised if it doesn't work! I don't know whether the spaces are legal, but I would leave them out to keep in line with the documentation.
You should change that statement to
my $dsn = "DBI:mysql:database=$database;host=$driver"
You may remove ;host=$driver if you wish (why have you called the host name "driver"?) as localhost is the default. A DSN that specifies just a database name and uses the default for all the other fields may be contracted to just
my $dsn = "DBI:mysql:$database"
It may be easier to just write print statements at first to generate some output. You will want to print a MIME content type header of text/plain instead of text/html. Try print "$DBI::errstr\n" for now instead of die, as the latter writes to stderr which won't appear in your browser
you could add:
if ($dbh) {
print 'connect ok';
} else {
print 'connect failed';
}

Cannot connect to database using local server, getting a blank white screen

So I just started building a basic site in preparation for my internship in the coming weeks, and I installed MAMP and everything was working fine. Set up my database and began designing the site, then for some reason I just got a blank white screen.
I tried refreshing, restarting the browser and restarting MAMP and continue to get the same blank page. At one point I got a message saying it could not connect to the database but I can't reproduce it at the moment. When I did localhost:8889 to access the MySQL port I received a download page with the following text:
J
5.5.34TW8Qp]bVˇ˜Äe|]<.Utj7oNt}mysql_native_password!ˇÑ#08S01Got packets out of order
(added a period after the <. because it wouldn't show the rest)
I am new to this so I am not really sure what to do from here. Please explain as if you are talking to someone with no knowledge on the subject, I took a couple of courses last semester but never got into hands-on stuff like this.
Try with this code.
<?php
$con = mysql_connect('database host', 'user name', 'password'); //Database host may be:127.0.0.1:3306
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('database name', $con);
$sql="set names 'utf8'";// for unicode support, must use after mysql_select_db() function.
mysql_query($sql);
mysql_query('SET CHARACTER SET utf8')or die('charset problem:'.mysql_error());
mysql_query("SET SESSION collation_connection ='utf8_general_ci'") or die('collation problem:'.mysql_error());
?>
Thanks.

Using UTF and Hindi in CakePHP and MySQL

I've create a form that contains Hindi (UTF-8) data which i want to store in MySQL table. The columns corresponding to UTF data has collation value set to utf_general_ci.
I've successfully stored the data in table but when I'm executing a select-where query, it doesn't returns the data. Here is my query:
SELECT Birth.sno, Birth.bookingnumber, Birth.birth_date, Birth.baby_gender, Birth.baby_name, Birth.baby_father_name, Birth.baby_father_address, Birth.baby_mother_name, Birth.birth_place, Birth.place_type, Birth.applicant_name, Birth.applicant_address, Birth.registration_number, Birth.registration_date, Birth.registration_ward, Birth.registration_city_village, Birth.registration_district, Birth.remark, Birth.mother_place_name, Birth.mother_place_type, Birth.mother_place_district, Birth.mother_place_state, Birth.person_religion, Birth.father_education, Birth.mother_education, Birth.father_occupation, Birth.mother_occupation, Birth.mother_age_at_marriage, Birth.mother_age_at_birth, Birth.count_of_mother_child, Birth.birth_by, Birth.birth_method, Birth.mother_weight_at_birth, Birth.pregnancy_duration, Birth.date_of_issue FROM np.births AS Birth WHERE Birth.baby_name = 'd' AND Birth.baby_father_name = 'e' AND Birth.baby_mother_name = 'f' AND Birth.baby_father_address = 'g' AND Birth.person_religion = 'हिंदू' AND Birth.baby_gender = 'पुरुष'
The name of the database is np and name of the table is births
The above query was printed in the log file. I tried to copy and paste the same query in HeidiSQL (front end for MySQL) but its not running. However, if I remove the following part: ** AND Birth.person_religion = 'हिंदू' AND Birth.baby_gender = 'पुरुष'**, the query works fine.
How can I resolve this issue?
This looks like a case when your MySQL client and your MySQL server do not "talk" the same encoding.
There are 3 places where you need to take care of your encoding.
The Web Form (what the users sees) -> Your Web Application (CakePHP) -> Your Database Server (MySQL)
One of those three is NOT using the same encoding as the others. So by the time:
"'हिंदू'" and "'पुरुष'" get to your database they will be something totally different that will not be found in the database.
So, make sure that in your default.ctp file you have set your encoding:
echo $this->Html->charset(); //this will result in a UTF-8 encoding of the page.
Look at the source code of your web page (where I guess you have a search/filter form).
At the top you should see:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Then look for the code generated for your search/filter form. You should see:
<form id="your_form_id" accept-charset="utf-8" method="post" action="/your/action/">
The important part is that it that "utf-8" that MUST show up in those places.
Next, look into your database.php file and make sure this line:
'encoding' => 'utf8', is NOT commented out!
Finally, with a client that you are sure supports UTF-8 (probably HeidiSQL) have a look at your data table np.births and make sure that what data you have there actually makes sense! It's possible it got mangled because of the discrepancies in encoding before.
Once the data makes sense in the database you should be good to go!
IF this does not do it you, you'll have to read and thoroughly understand this article. Only then you will be able to locate where the problem is and get your encodings in sync.
(Obviously your PHP source files should be UTF-8 encoded as well...)

The database does not collect the content other than English [Charset/Unicode set to UTF-8]

I have moved my server from dreamhost to Godaddy recently
When I was with Dreamhost it was no problem when my visitors submitting a form in my website using other language than English.
For example they use Thai language when they submit the form I receive it with no problem.
but what happen now when they submit any form I will receive a text like ?????????? so I though it might be just the setting on the php.ini file if that was set correctly and I see it was using UTF-8
; PHP's built-in default is text/html
default_mimetype = "text/html"
default_charset = "UTF-8"
and
exif.encode_unicode = "UTF-8"
exif.decode_unicode_motorola = "UCS-2BE"
exif.decode_unicode_intel = "UCS-2LE"
;exif.encode_jis =
exif.decode_jis_motorola = JIS
exif.decode_jis_intel = JIS
but I can't figure out why this is happen while all was set correctly
and each of my html/php header file I have included
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Any suggestion what I missed?
To put some more information that
If I use the FTP client such as Filezilla there will be another problem that when I edit the file .php or .html the content in the file if it has any other language than English it will go all like question mark ??????? but if I edit it from the web like Net2ftp then it has no problem on that
I had a similar problem with PHP + MySQL, MySQL refused to properly store UTF8 strings. The solution was to tell MySQL each time after connection, to use UTF8. I believe the SQL command to do so was SET CHARACTER SET UTF8, or SET NAMES UTF8 but I'm not sure, it was a long ago.
Edit: Found some old PHP code of mine, I hope it is helpful:
function sql_connect(){
global $host,$user,$pass,$dbname,$conn;
// connect to mysql
$conn=mysql_connect($host,$user,$pass);
// if cannot connect, exit
if(!$conn){
die('Cannot connect to MySQL server: '.mysql_errno().' - '.mysql_error());
};
// if cannot set to utf8, exit
if(!mysql_query('set names utf8;',$conn)){
mysql_close($conn);
die('Cannot set to UTF8: '.mysql_errno().' - '.mysql_error());
};
// if cannot select database, exit
if(!mysql_select_db($dbname,$conn)){
mysql_close($conn);
die('Cannot select database: '.mysql_errno().' - '.mysql_error());
};
};
function sql_to_html($x){
return(htmlspecialchars(stripslashes($x),ENT_QUOTES,"UTF-8"));
};
Don't forget to set PHP's internal encoding to UTF8 using iconv_set_encoding. Your php.ini preferences only affect the HTTP headers that are sent with your document! Finally, make sure you pass the encoding to mangling functions like htmlentities.

UTF-8 MySQL and Charset

Can someone explain me when I set everything to UTF-8 I keep getting those damn ���
MySQL
Server version: 5.1.44
MySQL charset: UTF-8 Unicode (utf8)
I create a new database
name: utf8test
collation: utf8_general_ci
MySQL connection collation: utf8_general_ci
My SQL looks like this:
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
CREATE TABLE IF NOT EXISTS `test_table` (
`test_id` int(11) NOT NULL,
`test_text` text NOT NULL,
PRIMARY KEY (`test_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `test_table` (`test_id`, `test_text`) VALUES
(1, 'hééélo'),
(2, 'wööörld');
My PHP / HTML:
<?php
$db_conn = mysql_connect("localhost", "root", "") or die("Can't connect to db");
mysql_select_db("utf8test", $db_conn) or die("Can't select db");
// $result = mysql_query("set names 'utf8'"); // this works... why??
$query = "SELECT * FROM test_table";
$result = mysql_query($query);
$output = "";
while($row = mysql_fetch_assoc($result)) {
$output .= "id: " . $row['test_id'] . " - text: " . $row['test_text'] . "<br />";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="it" xmlns="http://www.w3.org/1999/xhtml" xml:lang="it">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>UTF-8 test</title>
</head>
<body>
<?php echo $output; ?>
</body>
</html>
Try to set charachter encoding after mysql_connect function like this:
mysql_query ("set character_set_client='utf8'");
mysql_query ("set character_set_results='utf8'");
mysql_query ("set collation_connection='utf8_general_ci'");
I set everything to UTF-8
Not quite.
You have to tell mysql your client's encoding.
As a matter of fact, you don't have to set up "everything" in utf-8. You can have your tables in latin1 and output in utf-8. Or contrary.
Very flexible.
But you have to set up client's encoding explicitly.
So, that's why it works with set names utf8. Because this query setting up client's encoding. And let Mysql know that data must be sent in utf-8. Pretty sensible, huh?
Also I have to mention your SQL dump. It needs same setting. Just SET NAMES somewhere at the top. Because you are sending these queries from some client too. And this client's encoding needs to be set up as well.
And one more thing to mention: be sure your server sending proper encoding in the Content-type header. You didn't set it to UTF-8 too.
I didn't see a "SET NAMES 'utf8';" query just after connecting to your database.
Try it, may work for you.
I would say that you forget to set the content type encoding of your PHP file to utf-8:
header('Content-Type: text/html; charset=utf-8');
Or is the encoding error within the MySQL database?
If only loading the data returns the wrong results, you can use the queries mentioned before or this line of code to enable UTF-8 for queries:
$mysqli->set_charset('utf8');
I hope that is what you needed.
take a look at the mysql_set_charset function. Perhaps you need to call it before you retreive the data.
You want to check the current charset using mysql_client_encoding and when needed mysql_set_charset. Or just never mind the checking and blindly go with setting.