/    Sign up×
Community /Pin to ProfileBookmark

Encoding while inserting in mysql

Hi all,,

I have a task to insert arabic string in database ..

If i inserted arabic from any form in the website,, the arabic is stored in arabic (normal).

But the problem is that in my form if i inserted arabic string,, it stored as a strange characters (bad encoding).

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@firesnakerNov 30.2013 — Check your database encoding. The database encoding must be specific to Arabic if you intend to store Arabic characters.
Copy linkTweet thisAlerts:
@gemswebsauthorDec 01.2013 — Check your database encoding. The database encoding must be specific to Arabic if you intend to store Arabic characters.[/QUOTE]

Well.. i am inserting in the same field..

When i insert arabic from the website form it appear well..

But when i insert from my form [B]( the same word into the same field in the database )[/B] it turns to bad encoding
Copy linkTweet thisAlerts:
@firesnakerDec 02.2013 — I see, well, encoding is a difficult problem. One way I can think of is to make sure all encoding are the same, from the html, php, apache and mysql.

So, check your html file vs the website html file for encoding meta tag. See if there is any different.

Then, if you are on different server, development vs production, check for the locale encoding environment.

Of course, since you say they are from the same database, checking mysql encoding is not necessary.

If, all the above fails, you have a tricky problem.

Google is not much help. But I found a link related to MySQL that maybe of interest:

https://www.bluebox.net/insight/blog-article/getting-out-of-mysql-character-set-hell

Good luck!
Copy linkTweet thisAlerts:
@NogDogDec 03.2013 — Here's pretty much everything I know about the topic: http://www.ebookworm.us/2008/10/03/utf8-in-php-and-mysql/
Copy linkTweet thisAlerts:
@unasAquilaDec 04.2013 — This can be a bit overkill but works well
<i>
</i>$mysqli = new mysqli('127.0.0.1', 'username', 'password', 'database');

//check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %sn", mysqli_connect_error());
exit();
}

$mysqli-&gt;query("SET character_set_connection = utf8");
$mysqli-&gt;query("SET character_set_client = utf8");
$mysqli-&gt;query("SET character_set_results = utf8");
$mysqli-&gt;query("SET COLLATION_CONNECTION = 'utf8';");
$mysqli-&gt;query("SET NAMES utf8");
$mysqli-&gt;set_charset("utf8");

$string = "&amp;#1605;&amp;#1585;&amp;#1581;&amp;#1576;&amp;#1575; &amp;#1571;&amp;#1589;&amp;#1583;&amp;#1602;&amp;#1575;&amp;#1574;&amp;#1610; &amp;#1580;&amp;#1610;&amp;#1583;&amp;#1577;";
//$string = urlencode($string); //can be good also

$query = "INSERT INTO <span><code>arabic_table</code></span> (<span><code>string</code></span>) VALUES ('{$string}')";
$mysqli-&gt;query($query);

$select = "SELECT <span><code>string</code></span> FROM <span><code>arabic</code></span>";
$querys = $mysqli-&gt;query($select);

if ($result = $querys) {
while ($row = $result-&gt;fetch_row()) {
mb_check_encoding($row[0], 'utf-8');
echo $row[0] . "n";
}
$result-&gt;close();
}


also rember to make sure that the display page has these
<i>
</i>&lt;html dir="rtl" lang="ar"&gt;
&lt;meta charset="utf-8"&gt;


and you should be ok
Copy linkTweet thisAlerts:
@gemswebsauthorDec 04.2013 — Thanks all,,

SET NAMES.... sloved it.

?
×

Success!

Help @gemswebs 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 5.3,
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,
)...