Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Arabic charset working with PHP and mySQL

Posted on 2008-06-19
6
Medium Priority
?
3,049 Views
Last Modified: 2012-05-05
Hi all,
Exact issue is failing to insert Arabic data into mySQL using PHP application.  Result appear as question marks ???????

1. The same code works fine on my local server.
2. The same mySQL query works fine using myPHPAdmin given by the hosting company.
3. Reading Arabic data from mySQL works perfect.

PHP code used to insert the data:


<?php
    session_start();
    require "Utils.php";
    $isloggedin = isset($_SESSION["isloggedin"]) && $_SESSION["isloggedin"];
    $user = false;
    $passord = false;
    if($isloggedin) {
        // TODO change to session vars
        $user = "*********";
        $passord = "*********";
    } else {
        $user = "********";
        $passord = "********";
    }
    $con = mysql_connect("database name",$user,$password);
    if (!$con) {
        // TODO redirect to error page
        die('Could not connect: ' . mysql_error());
        exit;
    }
    
    // building the sql columns
    $sql_columns["event_type"] = $_POST["ddl_event_type"]; // int value
    $sql_columns["description"] = "'"  . $_POST["txtbox_description"] . "'";
    $sql_columns["event_days"] = "'" . $_POST["txtbox_days"] . "'";
    $sql_columns["event_time"] = "'" . $_POST["txt_time"] . "'";
    $sql_columns["event_location"] = "'" . $_POST["txt_location"] . "'";
    $sql_columns["event_map_url"] = "'" . $_POST["txt_map_url"] . "'";
    $sql_columns["event_details_url"] = "'" . $_POST["txt_details_url"] . "'";
    $sql_columns["added_by"] = "'" . $_POST["txt_owner"] . "'";
    $now = date("Y-m-d H:i:s");
    $sql_columns["added_date"] = "'" . $now . "'";
    if($isloggedin) {
        $sql_columns["accepted_date"] = "'" . $now . "'";
        $sql_columns["accepted"] = "1"; // boolean
        $sql_columns["expiration_date"] = "'" . $_POST["txt_exp_date"] . "'";
    }
    mysql_query("SET NAMES 'utf8'");
    mysql_query('SET CHARACTER SET utf8'); 
    mysql_query("SET character_set_results='utf8'");
    mysql_select_db("burair_munasabat", $con);
    
    $query = "INSERT INTO munasabat_table (";
    foreach($sql_columns as $colName => $colValue) {
        $query .= $colName . ",";
    }
    // remove last comma
    $query = substr($query, 0, strlen($query) - 1);
    $query .= ") VALUES(";
    foreach($sql_columns as $colName => $colValue) {
        $query .= iconv("windows-1256", "utf-8", $colValue) . ",";
    }
    // remove last comma
    $query = substr($query, 0, strlen($query) - 1);
    $query .= ")";
    echo $query;
    mysql_query($query);
    if(mysql_error()) {
        echo mysql_error();
    }
    mysql_close($con);
?>
 
<html dir="rtl">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <head>
    </head>
<body> Submitted </body>
</html>

Open in new window

0
Comment
Question by:The14follower
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 9

Expert Comment

by:Rurne
ID: 21824707
Sounds like your table collation's off.  What you need is iso-8859-6 character set support.  What MySQL actually includes is cp1256, which is called 'Windows Arabic.'  It should work okay with the following update to your database:

ALTER DATABASE `database name` DEFAULT CHARACTER SET = cp1256;
ALTER DATABASE  `database name` DEFAULT COLLATION = cp1256_general_ci;
0
 
LVL 1

Expert Comment

by:webjema
ID: 21829260
Are your database, tables and fields UTF-8?
0
 

Author Comment

by:The14follower
ID: 21831740
Rurne,
Your suggestion didn't work.  Again it works only on my local machine but not on the hosting server.

webjema,
Yes, all using utf-8.
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 1

Expert Comment

by:webjema
ID: 21833134
try remove convention
iconv("windows-1256", "utf-8", $colValue)
your data have been already utf-8!
0
 

Accepted Solution

by:
The14follower earned 0 total points
ID: 21833258
Thanks all,

I added these two lines and got it working:

mysql_query("SET character_set_connection='utf8'");
mysql_query("SET collation_connection='utf8_general_ci'");
0
 

Expert Comment

by:lowee81
ID: 22013355
I've faced the same problem
you can solve it by change it to utf-8 even in the html files

and even when you enter the Arabic words they will be stored exactly and all the words can be modified in all operating systems not only windows
0

Featured Post

Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question