Friday, October 28, 2011

How to write arabic text in mysql and show it with php

 

The database, the tables and columns had been defined with

(mysql) CHARACTER SET utf8 COLLATE utf8_general_ci;


The server would send a header telling the browser to expect utf-8

(php) header('Content-Type: text/html; charset=utf-8' );

The HTML page contained the necessary meta-tag

(html)

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

And important thing to type this code after connection

mysql_query("SET NAMES 'utf8'");

 

Example:database “quiz” table “math” field “question”

 <?php header("Content-type: text/html; charset=utf-8");  
//SQL Connection Info - update with your database, username & password
echo "بسم الله";
$connection = mysql_connect("localhost", "root","" ) or die ('cannot reach database');
mysql_query("SET NAMES 'utf8'");
$db = mysql_select_db("quiz") or die ("this is not a valid database");
//Change this query as you wish for single or multiple records
$result = mysql_query("SELECT * FROM math") or die('Query failed: ' . mysql_error());;
while ( $row = mysql_fetch_array($result) ) {
echo("<P>" . $row["question"] . "</P>");
}
?>

No comments:

Post a Comment