note 83717 added to function.mysql-set-charset

From: Date: Mon, 09 Jun 2008 05:11:28 +0000
Subject: note 83717 added to function.mysql-set-charset
Groups: php.notes 
Request: Send a blank email to php-notes+get-140257@lists.php.net to get a copy of this message
Here's an example of how to use this feature : I'm using PHP 5.2.5 from http://oss.oracle.com/projects/php/ I wanted to store arabic characters as UTF8 in the database and as suggested in many of the google results, I tried using mysql_query("SET NAMES 'utf8'"); and mysql_query("SET CHARACTER SET utf8 "); but it did not work. Using the following, it worked flawlessly $link = mysql_connect('localhost', 'user', 'password'); mysql_set_charset('utf8',$link); Once this is set we need not manually encode the text into utf using utf8_encode() or other functions. The arabic ( or any UTF8 supported ) text can be passed directly to the database and it is automatically converted by PHP. For eg. <?php $link = mysql_connect('localhost', 'user', 'password'); mysql_set_charset('utf8',$link); $db_selected = mysql_select_db('emp_feedback', $link); if (!$db_selected) { die ('Database access error : ' . mysql_error());} $query = "INSERT INTO feedback ( EmpName, Message ) VALUES ('$_empName','$_message')"; mysql_query($query) or die('Error, Feedback insert into database failed'); ?> Note that here $_empName is stored in English while $_message is in Arabic. ---- Server IP: 69.147.83.197 Probable Submitter: 89.211.33.44 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-set-charset.php Edit -- https://master.php.net/note/edit/83717 Del: integrated -- https://master.php.net/note/delete/83717/integrated Del: useless -- https://master.php.net/note/delete/83717/useless Del: bad code -- https://master.php.net/note/delete/83717/bad+code Del: spam -- https://master.php.net/note/delete/83717/spam Del: non-english -- https://master.php.net/note/delete/83717/non-english Del: in docs -- https://master.php.net/note/delete/83717/in+docs Del: other reasons-- https://master.php.net/note/delete/83717 Reject -- https://master.php.net/note/reject/83717 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#140257) next »