php, mysql & unicode
It all started when I wanted to make a website in Bangla. The best way was to use unicode. First of all, I had to install mysql-4.1.22-win32.zip because the older versions did not support unicode or it supported in a very funny way. Use php 4.3.8
To keep things simple and easy to understand open the mysql command prompt.
mysql> set names ‘utf8′;
mysql> create database baka character set utf8 collate utf8_general_ci;
mysql> use baka;
mysql> create table haka (mala varchar(100) character set utf8 collate utf8_general_ci);
The database is created and one table has also been created.
Now for the php part. Create a file called index.php and write the following in it
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<HTML>
<HEAD>
<TITLE> Unicode </TITLE>
<META NAME=”Generator” CONTENT=”Microsoft FrontPage 5.0″>
<meta http-equiv=”Content-Language” content=”bn”>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ >
</HEAD>
<?
$c = mysql_connect(“localhost”,”root”,”iit123″);
mysql_select_db(‘baka’);
echo mysql_error();
mysql_query(‘SET CHARACTER SET utf8′);
mysql_query(“SET SESSION collation_connection =’utf8_general_ci’”);
echo mysql_error();
if(isset($_POST))
{
mysql_query(“insert into haka values(‘”.$_POST["username"].”‘)”);
echo “saved”;
echo mysql_error();
}
else
{
echo “not posted”;
}
$d = mysql_query(“select * from haka”,$c);
while($x = mysql_fetch_array($d))
{
echo $x["mala"].”<br>”;
}
?>
<form action=”index.php” name=”frm” method=”post”>
<input type=”text” name=”username” size=”20″>
<input type=”submit” value=”Login” name=”btnLogin”>
</form>
Thats all. Now you can see and save unicode in php and mysql.
It’s a shame what happened to Bangladesh. I hope the world steps up and helps them.
retro
November 20, 2007
Thak you for good tutorial.
Articles Host
January 10, 2008
very helpful.
Imran
June 8, 2009