/    Sign up×
Community /Pin to ProfileBookmark

Arabic Text In MySQL Table ?

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
——————————

[CODE]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;[/CODE]

thanx alot guys for the help..

Regards,

Noor

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 22.2006 — What versions of PHP and MySQL are you using?
Copy linkTweet thisAlerts:
@aaronbdavisMar 22.2006 — 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.
Copy linkTweet thisAlerts:
@nooor83authorMar 25.2006 — hi guys,

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

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

the table is also fine.

[CODE]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;[/CODE]


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
Copy linkTweet thisAlerts:
@nooor83authorMar 25.2006 — 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
Copy linkTweet thisAlerts:
@nooor83authorMar 27.2006 — 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 [CODE]mb_detect_encoding($data)[/CODE], it gave me ASCII..

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

thanx alot again guys for the help

Regards,

Noor
Copy linkTweet thisAlerts:
@nooor83authorMar 28.2006 — hi gusy,

i got the answer..

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

[CODE]mysql_query("set characer set cp1256");[/CODE]

but why ??

Regards,

Noor
Copy linkTweet thisAlerts:
@fares1979Nov 10.2008 — Mr Noor,

ihave the same problem if you get the solution plz sent it to me..

[mod]no contact information[/mod]
Copy linkTweet thisAlerts:
@mkiredjianFeb 26.2009 — 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.
Copy linkTweet thisAlerts:
@UltimaterMar 01.2009 — This worked perfectly for me:
[code=php]
<?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;
?>
[/code]


[code=php]
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=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

INSERT INTO poll_categories (category_id, category_name) VALUES
(1, '&#1604;&#1576;&#1606;&#1575;&#1606; &#1610;&#1575; &#1576;&#1604;&#1583; &#1589;&#1594;&#1610;&#1585; &#1576;&#1575;&#1604;&#1602;&#1604;&#1576; &#1603;&#1576;&#1610;&#1585;');
[/code]
Copy linkTweet thisAlerts:
@coloradorockiesMar 23.2011 — 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.
Copy linkTweet thisAlerts:
@yasser1820Aug 22.2011 — i am using wamp server (windows,apache,mysql &php).

[B]//so important[/B]

[COLOR="Lime"]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 D? 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).[/COLOR]



[B]//i tried this but i didn't see effect.[/B]

[COLOR="Yellow"]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" ...>[/COLOR]
Copy linkTweet thisAlerts:
@UltimaterAug 23.2011 — Give this a shot:
<i>
</i>&lt;?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,$htmlInsertDataResult)=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&lt;br /&gt;n&lt;a href="{$_SERVER['PHP_SELF']}"&gt;Return to main page.&lt;/a&gt;";
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 <span><code>arabic_test_table1</code></span> (<span><code>id</code></span>, <span><code>arabic_text</code></span>) 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&lt;br /&gt;n";
}else if($num&gt;1){
$addEntryResult.="Successfully added your &lt;em&gt;posts&lt;/em&gt; to the database!n&lt;br /&gt;n";
}else{
$addEntryResult.="An unknown error occured while trying to add your post to the database.n&lt;br /&gt;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 <span><code>arabic_test_table1</code></span>');
$outTable="";
$outTable.='&lt;table border="1"&gt;&lt;tr&gt;';
while($row=mysql_fetch_assoc($result))
{
$outTable.='&lt;th&gt;'.htmlentities($row['Field']).'&lt;/th&gt;';
}//while
$outTable.='&lt;/tr&gt;'; <br/>
$result=mysql_query('SELECT * FROM <span><code>arabic_test_table1</code></span>');
while($row=mysql_fetch_row($result))
{
$outTable.='&lt;tr&gt;';
foreach($row as $column)
{
$outTable.='&lt;td&gt;'.htmlentities($column,ENT_QUOTES,'UTF-8').'&lt;/td&gt;';
}
$outTable.='&lt;/tr&gt;';
}//while
$outTable.="&lt;/table&gt;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 <span><code>arabic_test_table1</code></span> (
<span><code>id</code></span> int(10) unsigned NOT NULL auto_increment,
<span><code>arabic_text</code></span> text character set utf8 NOT NULL,
PRIMARY KEY (<span><code>id</code></span>),
FULLTEXT KEY <span><code>arabic_text</code></span> (<span><code>arabic_text</code></span>)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2;
-- ---------sql separator----------
INSERT INTO <span><code>arabic_test_table1</code></span> (<span><code>id</code></span>, <span><code>arabic_text</code></span>) VALUES
(1, '&amp;#1604;&amp;#1576;&amp;#1606;&amp;#1575;&amp;#1606; &amp;#1610;&amp;#1575; &amp;#1576;&amp;#1604;&amp;#1583; &amp;#1589;&amp;#1594;&amp;#1610;&amp;#1585; &amp;#1576;&amp;#1575;&amp;#1604;&amp;#1602;&amp;#1604;&amp;#1576; &amp;#1603;&amp;#1576;&amp;#1610;&amp;#1585;');
======data separator======
?&gt;
&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;Ultimater's Arabic Test Database Example&lt;/title&gt;
&lt;link rel="icon" href="http://www.ultimater.net/favicon.ico" type="image/x-icon"/&gt;
&lt;link rel="shortcut icon" href="http://www.ultimater.net/favicon.ico" type="image/x-icon"/&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action="&lt;?php echo $_SERVER['PHP_SELF'];?&gt;" method="POST"&gt;
&lt;input type="submit" name="action" value="Create Table" /&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;?php
======data separator======
?&gt;
&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;Ultimater's Arabic Test Database Example&lt;/title&gt;
&lt;link rel="icon" href="http://www.ultimater.net/favicon.ico" type="image/x-icon"/&gt;
&lt;link rel="shortcut icon" href="http://www.ultimater.net/favicon.ico" type="image/x-icon"/&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php
echo "Table created successfully!n&lt;br /&gt;n&lt;a href="{$_SERVER['PHP_SELF']}"&gt;Return to main page.&lt;/a&gt;";
?&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;?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======
?&gt;
&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;Ultimater's Arabic Test Database Example&lt;/title&gt;
&lt;link rel="icon" href="http://www.ultimater.net/favicon.ico" type="image/x-icon"/&gt;
&lt;link rel="shortcut icon" href="http://www.ultimater.net/favicon.ico" type="image/x-icon"/&gt;
&lt;style type="text/css"&gt;&lt;?php echo $cssData; ?&gt;&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h2&gt;Table <span><code>arabic_test_table1</code></span>:&lt;/h2&gt;
&lt;?php echo $outTable; ?&gt;
&lt;br /&gt;
&lt;a href="&lt;?php echo $_SERVER['PHP_SELF'];?&gt;?action=Insert Data"&gt;Insert Data&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;?php
======data separator======
?&gt;
&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;Ultimater's Arabic Test Database Example&lt;/title&gt;
&lt;link rel="icon" href="http://www.ultimater.net/favicon.ico" type="image/x-icon"/&gt;
&lt;link rel="shortcut icon" href="http://www.ultimater.net/favicon.ico" type="image/x-icon"/&gt;
&lt;style type="text/css"&gt;&lt;?php echo $cssData; ?&gt;&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h2&gt;Insert Data into <span><code>arabic_test_table1</code></span>:&lt;/h2&gt;
&lt;form action="&lt;?php echo $_SERVER['PHP_SELF'];?&gt;" method="POST"&gt;
&lt;input type="hidden" name="action" value="Insert Data" /&gt;
&lt;input type="hidden" name="subaction" value="submit" /&gt;
&lt;input type="text" name="stringToSave" value="" /&gt;
&lt;input type="submit" value="Insert!" /&gt;
&lt;/form&gt;
&lt;?php echo "&lt;a href="{$_SERVER['PHP_SELF']}"&gt;Return to main page.&lt;/a&gt;"; ?&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;?php
======data separator======
?&gt;
&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;Ultimater's Arabic Test Database Example&lt;/title&gt;
&lt;link rel="icon" href="http://www.ultimater.net/favicon.ico" type="image/x-icon"/&gt;
&lt;link rel="shortcut icon" href="http://www.ultimater.net/favicon.ico" type="image/x-icon"/&gt;
&lt;style type="text/css"&gt;&lt;?php echo $cssData; ?&gt;&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php echo $addEntryResult; ?&gt;
&lt;?php echo "&lt;a href="{$_SERVER['PHP_SELF']}"&gt;Return to main page.&lt;/a&gt;"; ?&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;?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.

[upl-file uuid=a3e0afe4-a58c-480a-a5c5-c8ff8f4e1913 size=7kB]arabic_test.php.txt[/upl-file]
Copy linkTweet thisAlerts:
@UltimaterAug 23.2011 — Also make sure there is white space after the final <?php

If your editor strips them, add a comment afterwards like:
<i>
</i>&lt;?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
×

Success!

Help @nooor83 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...