www.webdeveloper.com
Results 1 to 7 of 7

Thread: Encoding while inserting in mysql

  1. #1
    Join Date
    Jul 2013
    Posts
    18

    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).

  2. #2
    Join Date
    Nov 2012
    Location
    Jakarta
    Posts
    42
    Check your database encoding. The database encoding must be specific to Arabic if you intend to store Arabic characters.

  3. #3
    Join Date
    Jul 2013
    Posts
    18
    Quote Originally Posted by firesnaker View Post
    Check your database encoding. The database encoding must be specific to Arabic if you intend to store Arabic characters.
    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 ( the same word into the same field in the database ) it turns to bad encoding

  4. #4
    Join Date
    Nov 2012
    Location
    Jakarta
    Posts
    42
    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...acter-set-hell

    Good luck!

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    Here's pretty much everything I know about the topic: http://www.ebookworm.us/2008/10/03/u...php-and-mysql/
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Nov 2013
    Posts
    33
    This can be a bit overkill but works well
    Code:
    $mysqli = new mysqli('127.0.0.1', 'username', 'password', 'database');
    
    //check connection 
    if (mysqli_connect_errno()) { 
        printf("Connect failed: %s\n", mysqli_connect_error()); 
        exit(); 
    }
    
    $mysqli->query("SET character_set_connection = utf8"); 
    $mysqli->query("SET character_set_client = utf8"); 
    $mysqli->query("SET character_set_results = utf8");
    $mysqli->query("SET COLLATION_CONNECTION = 'utf8';");
    $mysqli->query("SET NAMES utf8");
    $mysqli->set_charset("utf8");
    
    $string = "مرحبا أصدقائي جيدة";
    //$string = urlencode($string); //can be good also
    
    $query = "INSERT INTO `arabic_table` (`string`) VALUES ('{$string}')";
    $mysqli->query($query);
    
    $select = "SELECT `string` FROM `arabic`";
    $querys = $mysqli->query($select);
    
    if ($result = $querys) { 
        while ($row = $result->fetch_row()) { 
            mb_check_encoding($row[0], 'utf-8'); 
            echo $row[0] . "\n"; 
        } 
        $result->close(); 
    }
    also rember to make sure that the display page has these
    Code:
    <html dir="rtl" lang="ar">
    	<meta charset="utf-8">
    and you should be ok

  7. #7
    Join Date
    Jul 2013
    Posts
    18
    Thanks all,,

    SET NAMES.... sloved it.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles