0

i have a mysql database that stores the arabic in it, it works fine in both savng the arabic characters and retrieving the content, the probem i am receiving is when i retrieve this arabic content from the database and want to use in another query it does not work:

    //Database Connection
     mysql_query("SET CHARACTER SET utf8");
     $sql = mysql_query("SELECT * FROM `categories` WHERE 1");

     while($row1 = mysql_fetch_array($sql))
     {
        $category =  $row1['label_id']; //Arabic Content is retrieved displayed correctly
        $query1 = mysql_query("SELECT * FROM `user_list` WHERE `Category` = '" .$category . "' ORDER BY `private_number` DESC");
        $query1 = mysql_query("SET CHARACTER SET utf8");
        while($row = mysql_fetch_array($query1))
        {
           //returns zero rows
        }
     }

but when i copy the string echo above and query without concatenating it does work very well example:

$query1 = mysql_query("SELECT * FROM `user_list` WHERE `Category` = 'الأفراد' ORDER BY   `private_number` DESC");

What is wrong with my php code and what can i do to change it

H2ONOCK
  • 956
  • 1
  • 5
  • 19
  • Outside not saving strange char to db but only their html entity? – Marco Mura Dec 17 '14 at 10:04
  • in the database it save the unicode characters correctly, not their html equivalent –  Dec 17 '14 at 10:09
  • 1
    Possible Duplicate of http://stackoverflow.com/questions/279170/utf-8-all-the-way-through - though not golden-hammer voting it closed. – Fluffeh Dec 17 '14 at 10:10
  • Won't the above just run 'SET CHARACTER SET....' repeatedly as this definition of the $query1 variable overwrites the previous one? Maybe you meant to use $query1 .= ? I'd think the CHARACTER SET query should go before the SELECT query too. – H2ONOCK Dec 17 '14 at 10:14
  • If you're just learning PHP, please, do not learn the obsolete `mysql_query` interface. It's awful and is being removed in future versions of PHP. A modern replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/). A guide like [PHP The Right Way](http://www.phptherightway.com/) can help explain best practices. Always be absolutely **sure** your user parameters are [properly escaped](http://bobby-tables.com/php) or you will have severe [SQL injection bugs](http://bobby-tables.com/). – tadman Dec 17 '14 at 10:33

1 Answers1

0
  • Make sure that both Database and Tables Collation is set to utf8_general_ci. if not, use a tool like this to convert collation after backing up the database.

  • The encoding of the files should be UTF-8 as well.

  • Also, check that Meta tag content-type is set to utf-8: <meta charset="utf-8"> for HTML5, OR <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> for XHTML.

wesamly
  • 1,484
  • 1
  • 16
  • 23