Friday 31 August 2018

The code does not print the Arabic text of the database

The code below that I am using will not print text that is of arabic form from the database although the collation of the table is uft8_general_ci and the database is of collation uft8_general_ci.

code:
<?php

        // Create connection
    $con = mysqli_connect("localhost", "", "", "");
    mysqli_set_charset('utf8', $con);

    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    // This SQL statement selects ALL from the table 'Locations'
    $sql = "SELECT * FROM Mothakirat";
    mysqli_set_charset('utf8', $sql);

    // Check if there are results
    if ($result = mysqli_query($con, $sql))
    {
        // If so, then create a results array and a temporary one
        // to hold the data
        $resultArray = array();
        $tempArray = array();

        // Loop through each row in the result set
        while ($row = $result->fetch_object())
        {
            // Add each row into our results array
            $tempArray = $row;
            array_push($resultArray, $tempArray);
        }

        // Finally, encode the array to JSON and output the results
        echo htmlentities(json_encode($resultArray));
    }

    // Close connections
    mysqli_close($con);

?>​

How can I get the arabic to print properly?

When working with non-Latin characters in a web based environment, I find there are three points of failure:
1) You need to tell mysql what output to give you when you connect (which you've already done, I think):
mysqli_query('SET CHARACTER SET utf8');

2) You probably need to add
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

into the <head> section in your html.
3) You need to ensure that the font used to display the text has glyphs for the characters you are expecting to see (Times New Roman may not suffice). I have done stuff with Greek and Hebrew and I often find fonts have basic support but not when there are diacritics.

0 comments:

Post a Comment