Click to See Complete Forum and Search --> : Arabic Text In MySQL Table ?


nooor83
03-22-2006, 01:56 AM
hi guys,

i have a small problem..mysql supports arabic right..when i store data in tables it's alright..everything works fine ?? but when i issue an sql to retrieve data..i get the arabic text as ???? marks..!! i have changed the charset of the tables and of the columns but still the same problem ??

here is the design of the table
------------------------------

DROP TABLE IF EXISTS "noorbiz"."poll_categories";
CREATE TABLE "noorbiz"."poll_categories" (
"category_id" tinyint(2) NOT NULL auto_increment,
"category_name" varchar(50) character set utf8 collate utf8_bin NOT NULL default '',
PRIMARY KEY ("category_id")
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


thanx alot guys for the help..

Regards,

Noor

NogDog
03-22-2006, 11:02 AM
What versions of PHP and MySQL are you using?

aaronbdavis
03-22-2006, 01:56 PM
It may simply be a browser problem. Make sure you set the character encoding of the page to Unicode. Also, check the source of the page: If you see arabic text in the source, then it is a presentation problem.

nooor83
03-25-2006, 03:14 AM
hi guys,

im using php5 and mysql5 versions..the page is in encoding is

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

the table is also fine.

DROP TABLE IF EXISTS "noorbiz"."poll_categories";
CREATE TABLE "noorbiz"."poll_categories" (
"category_id" tinyint(2) NOT NULL auto_increment,
"category_name" varchar(50) character set utf8 collate utf8_bin NOT NULL default '',
PRIMARY KEY ("category_id")
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

the thing that making me crazy is that when i view the table in phpmyadmin im getting the proper arabic text ?? there must be a way they are using ??

thanx alot..

Regards,

Noor

nooor83
03-25-2006, 04:34 AM
hi again guys,,

to make it very clear..

i store arabic text in the table, that 's alright...and when i browse the table everything is alright..but when i try to retrieve the data from the db the arabic text is displayed as ???????? (question marks) on the page..

is there anything to do with the server configration ?

Thanx

Noor

nooor83
03-27-2006, 02:47 AM
guys please help..

im using WAMP server...has apache 2.x apache5 and mysql5

going mad here and dunno why arabic text displays as ????? when i retrieve it in an php page..

is it a server configuration problem or what ??

when i tried to detect the encoding..of the string im returning from the database using mb_detect_encoding($data), it gave me ASCII..

one more point...phpmyadmin displays the arabic text perfectly..

thanx alot again guys for the help

Regards,

Noor

nooor83
03-27-2006, 11:14 PM
hi gusy,

i got the answer..

you have to issue this query when after making the connection to db

mysql_query("set characer set cp1256");

but why ??

Regards,

Noor

fares1979
11-10-2008, 12:35 AM
Mr Noor,
ihave the same problem if you get the solution plz sent it to me..

no contact information

mkiredjian
02-26-2009, 07:17 AM
To read arabic from mysql through php

1) The field containing the arabic text should have Charset utf8 collation utf8_unicode_ci
2) In the page where you want to read this field
a)set the charset of the page to windows-1256
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
b) After establishing the connection to the MySQL write this two lines
mysql_query("SET NAMES cp1256");
mysql_query("set characer set cp1256");

This will work perfectly I have tested it.

Ultimater
03-01-2009, 01:49 AM
This worked perfectly for me:

<?php
header('content-type:text/html; charset=utf-8');
mysql_connect('localhost','root','');
mysql_select_db('noorbiz');
mysql_query("set names utf8");
list($id,$name)=mysql_fetch_row(mysql_query('SELECT * FROM poll_categories'));
echo $name;
?>



CREATE TABLE IF NOT EXISTS `poll_categories` (
`category_id` tinyint(2) NOT NULL auto_increment,
`category_name` varchar(50) character set utf8 collate utf8_bin NOT NULL default '',
PRIMARY KEY (`category_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

INSERT INTO `poll_categories` (`category_id`, `category_name`) VALUES
(1, 'لبنان يا بلد صغير بالقلب كبير');

coloradorockies
03-23-2011, 11:21 AM
I can't give you the details, but basically the text is converted to UTF codes. These are 16 bit (two byte) values. Each separate letter or symbol in any language is saved as a unique value. When you look at the raw data in the database, you are seeing the 8 bit (one byte) ASCII representations of the two halves of the real value. Hence, something like the two characters "Ø*" is in reality one Arabic symbol. When MySQL outputs the information, it is converted by the wiki from the two-byte values back to the proper Arabic symbols.

The same happens with any other non-European language. 8 bit ASCII has all the characters based on latin, Greek etc (many of the non-English letters are in the high order symbols; values between 0 and 127 are mostly English and the values from 128 to 255 include non-English symbols and letters). This reflects the US origins of ASCII. Chinese, Japanese, Arabic, and so many other languages have their text rendered with 16 bit values from 256 to 65384.

So - you should look up info on UTF character sets and how they are displayed.

Note there is UTF-8 and UTF-16. My understanding is that UTF-8 ensures that the values from 0-255 in the 16 bit format are the same as the same values in the 8-bit ASCII set.

yasser1820
08-22-2011, 10:28 AM
i am using wamp server (windows,apache,mysql &php).

//so important

first : phpmyadmin or MySQL it self (What ever u have) :
make sure that mysql DB is utf-8.
make sure that the db (u create) and the tables (u create) are utf-general-ci

after connecting to Mysl immidiately (before choosing DB) put this order.
mysql_set_charset('utf8');

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

make sure that the script it self is utf-8 enabled. (usually in the bottom of the script , like in notepad++ , at the status bar , right side, or search in the IDE configuration u use).


//i tried this but i didn't see effect.

in the header of the php file (before every thing).
<?php header("Content-type: text/html; charset=utf-8"); ?>

//also didn't help .
in the form submitted:
<form accept-charset="utf-8" ...>

Ultimater
08-23-2011, 12:09 AM
Give this a shot:

<?php
//load plain/text data into $dataTokenCollection as a string.
$dataTokenCollection=file_get_contents(__FILE__,null,null,__COMPILER_HALT_OFFSET__);
list($sqlDataQueries,$htmlData,$htmlData2,$cssData,$htmlView,$htmlInsertDataPage,$htmlInsertDataResu lt)=explode('======data separator======',$dataTokenCollection);
$sqlData=explode('-- ---------sql separator----------',$sqlDataQueries);



mysql_connect('localhost','root','');
mysql_select_db('arabic_test');
mysql_set_charset('utf8');
$action=isset($_POST['action'])?$_POST['action']:(isset($_GET['action'])?$_GET['action']:'');
if($action=='Create Table')
{
mysql_query($sqlData[0]) or die(mysql_error());
mysql_query($sqlData[1]) or die(mysql_error());
mysql_query($sqlData[2]) or die(mysql_error());
//echo "Table created successfully!\n<br />\n<a href=\"{$_SERVER['PHP_SELF']}\">Return to main page.</a>";
eval($htmlData2);
exit;
}

if($action=='Insert Data')
{
if(!isset($_POST['subaction']))
{
eval($htmlInsertDataPage);
}else if($_POST['subaction']=='submit'){
if(!isset($_POST[stringToSave])){echo 'No data sent!';exit;}
$str=mysql_real_escape_string($_POST[stringToSave]);
$sql="INSERT INTO `arabic_test_table1` (`id`, `arabic_text`) VALUES (NULL,'$str')";
$result=mysql_query($sql) or die(mysql_error());
$num=mysql_affected_rows();
$addEntryResult="";
if($num==1)
{
$addEntryResult.="Successfully added your post to the database!\n<br />\n";
}else if($num>1){
$addEntryResult.="Successfully added your <em>posts</em> to the database!\n<br />\n";
}else{
$addEntryResult.="An unknown error occured while trying to add your post to the database.\n<br />\n";
}
eval($htmlInsertDataResult);
}//else
exit;
}


if(mysql_num_rows(mysql_query("SHOW TABLES LIKE 'arabic_test_table1'"))==0)
{
eval($htmlData);//echos the results of the evaluated PHP code
}else{
$result=mysql_query('SHOW COLUMNS FROM `arabic_test_table1`');
$outTable="";
$outTable.='<table border="1"><tr>';
while($row=mysql_fetch_assoc($result))
{
$outTable.='<th>'.htmlentities($row['Field']).'</th>';
}//while
$outTable.='</tr>';
$result=mysql_query('SELECT * FROM `arabic_test_table1`');
while($row=mysql_fetch_row($result))
{
$outTable.='<tr>';
foreach($row as $column)
{
$outTable.='<td>'.htmlentities($column,ENT_QUOTES,'UTF-8').'</td>';
}
$outTable.='</tr>';
}//while
$outTable.="</table>\n";
eval($htmlView);//echos the results of the evaluated PHP code
}//else



//Everything after the halt complier will be ignored by PHP
//But in this case we will have PHP open itself in plain/text mode later to grab this data
__halt_compiler();
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
-- ---------sql separator----------
CREATE TABLE IF NOT EXISTS `arabic_test_table1` (
`id` int(10) unsigned NOT NULL auto_increment,
`arabic_text` text character set utf8 NOT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `arabic_text` (`arabic_text`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2;
-- ---------sql separator----------
INSERT INTO `arabic_test_table1` (`id`, `arabic_text`) VALUES
(1, 'لبنان يا بلد صغير بالقلب كبير');
======data separator======
?>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Ultimater's Arabic Test Database Example</title>
<link rel="icon" href="http://www.ultimater.net/favicon.ico" type="image/x-icon"/>
<link rel="shortcut icon" href="http://www.ultimater.net/favicon.ico" type="image/x-icon"/>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<input type="submit" name="action" value="Create Table" />
</form>
</body>
</html>
<?php
======data separator======
?>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Ultimater's Arabic Test Database Example</title>
<link rel="icon" href="http://www.ultimater.net/favicon.ico" type="image/x-icon"/>
<link rel="shortcut icon" href="http://www.ultimater.net/favicon.ico" type="image/x-icon"/>
</head>
<body>
<?php
echo "Table created successfully!\n<br />\n<a href=\"{$_SERVER['PHP_SELF']}\">Return to main page.</a>";
?>
</form>
</body>
</html>
<?php
======data separator======
body {
margin: 0;
padding: 1em;
font: medium arial, helvetica, sans-serif;
}
table {
border-collapse: collapse;
border: solid 2px black;
font-weight: normal;
font-size: small;
margin: 0.25em 0 0.5em 0;
}
td,th {
border: solid 1px black;
padding: 0.1em 0.25em;
}
th { background-color: #ccc; }
h2 {
/* CSS Horizontal Rule */
margin: 1em 0 0.5em 0;
border-top: solid 2px black;
padding-top: 1em;
}
li {
font-weight: bold;
font-size: large;
margin-top: 0.5em;
}
======data separator======
?>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Ultimater's Arabic Test Database Example</title>
<link rel="icon" href="http://www.ultimater.net/favicon.ico" type="image/x-icon"/>
<link rel="shortcut icon" href="http://www.ultimater.net/favicon.ico" type="image/x-icon"/>
<style type="text/css"><?php echo $cssData; ?></style>
</head>
<body>
<h2>Table `arabic_test_table1`:</h2>
<?php echo $outTable; ?>
<br />
<a href="<?php echo $_SERVER['PHP_SELF'];?>?action=Insert Data">Insert Data</a>
</body>
</html>
<?php
======data separator======
?>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Ultimater's Arabic Test Database Example</title>
<link rel="icon" href="http://www.ultimater.net/favicon.ico" type="image/x-icon"/>
<link rel="shortcut icon" href="http://www.ultimater.net/favicon.ico" type="image/x-icon"/>
<style type="text/css"><?php echo $cssData; ?></style>
</head>
<body>
<h2>Insert Data into `arabic_test_table1`:</h2>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<input type="hidden" name="action" value="Insert Data" />
<input type="hidden" name="subaction" value="submit" />
<input type="text" name="stringToSave" value="" />
<input type="submit" value="Insert!" />
</form>
<?php echo "<a href=\"{$_SERVER['PHP_SELF']}\">Return to main page.</a>"; ?>
</body>
</html>
<?php
======data separator======
?>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Ultimater's Arabic Test Database Example</title>
<link rel="icon" href="http://www.ultimater.net/favicon.ico" type="image/x-icon"/>
<link rel="shortcut icon" href="http://www.ultimater.net/favicon.ico" type="image/x-icon"/>
<style type="text/css"><?php echo $cssData; ?></style>
</head>
<body>
<?php echo $addEntryResult; ?>
<?php echo "<a href=\"{$_SERVER['PHP_SELF']}\">Return to main page.</a>"; ?>
</body>
</html>
<?php

(see also attached attachment in case you have problems saving in UTF-8 mode)

Make sure to create a database `arabic_test` before running the code.

Ultimater
08-23-2011, 02:11 AM
Also make sure there is white space after the final <?php
If your editor strips them, add a comment afterwards like:

<?php
//comment to preserve the whitespace after the opening PHP tag


and finally here's a test page:
http://ultimater.net/public/arabic_test.php