2

I want to bring Arabic data from mysql database I wrote the php code but it gives me ????? on arabic data any help to make it wokrs ??

<?php header('Content-Type: charset=utf-8'); 
$link=mysqli_connect("localhost","root","","arabicd");
mysql_set_charset('utf8',$link);
if (mysqli_connect_errno($link))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysql_query("SET character_set_results = 'utf8'");
mysql_query("character_set_client = 'utf8'"); 
mysql_query("character_set_connection = 'utf8'");  
mysql_query("character_set_database = 'utf8'");
$result = mysqli_query($link,"SELECT question,answer FROM ask ");
while ($row = mysqli_fetch_array($result))
{
$output[]=$row;
}
print(json_encode($output));

if($data){
echo $data;
}
mysqli_close($con);
?>
Najah Mousa
  • 23
  • 1
  • 5
  • You're mixing `mysql_*` and `mysqli_*`. Stick with mysqli for everything. – Mike May 29 '15 at 03:36
  • I did still not working :\ – Najah Mousa May 29 '15 at 03:43
  • Please post error details.. – Arshid KV May 29 '15 at 03:43
  • Warning: mysqli_set_charset() expects parameter 1 to be mysqli, string given in C:\wamp\www\ArabicCon.php on line 3 Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp\www\ArabicCon.php on line 9( ! ) Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp\www\ArabicCon.php on line 10 Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp\www\ArabicCon.php on line 11 Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp\www\ArabicCon.php on line 12 [{"0":"????","question":"????","1":"????","answer":"????"}] – Najah Mousa May 29 '15 at 03:45
  • @ArshidKV this is the errors appear ! – Najah Mousa May 29 '15 at 03:57
  • possible duplicate of [PHP MYSQL Insert Data in Arabic Language](http://stackoverflow.com/questions/11262965/php-mysql-insert-data-in-arabic-language) – Ansari May 29 '15 at 04:20

4 Answers4

2

Use this code in your PHP:

$db= mysqli_connect('localhost','root','225352','project');

$SQL= 'SET CHARACTER SET utf8';

mysqli_query($db,**$SQL**);) 
or die ( mysqli_error($SQL) );
Gustavo Morales
  • 2,614
  • 9
  • 29
  • 37
rs_
  • 83
  • 7
1

No, don't use the mysql interface, use mysqli.
No, don't use SET NAMES, use mysqli_set_charset.

Do SHOW CREATE TABLE; you will probably find that the column you are trying to write into is CHARACTER SET latin. It needs to be utf8.

Text like \u0633 implies that you need to add JSON_UNESCAPED_UNICODE as the second argument to json_encode.

Rick James
  • 135,179
  • 13
  • 127
  • 222
0
  • Though you can just use set names utf8 instead of all that creepy queries, there is nothing here wrong about Arabic, cheers :)
  • You are mixing mysql_* and mysqli_* which is not allowed
  • Even if it's allowed to use mysql & mysqli connections interchangeably, you are passing parameters toquery functions in an order that is different from expected
  • You should exit the script in case of connection failure (or do what ever, just change the execution flow)
  • That line print(json_encode($output)); is error prone, what if there are no results came from the table ? the loop will never be entered and $output will not be defined, so you must initialize $output with empty array, it's also a good practice to check if the result set is empty and handle it in a separate block
  • Also, it's preferred to enclose table names and columns in back ticks (`) in case that a column has a name of a reserved keyword

Here is a working version of your code after fixing errors

<html>

<meta charset="utf-8">
<?php 



$link = mysql_connect("localhost","root","");
if ($link === false)
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    exit;
}
mysql_select_db("arabicd", $link);
mysql_query("SET NAMES utf8");

$result = mysql_query("SELECT `ask`.`question`,`ask`.`answer` FROM `ask` ", $link);
$output = array();
while ($row = mysql_fetch_array($result))
{
    $output[]=$row;
}

print(json_encode($output));

if(isset($data))
{
 echo $data;
}

mysql_close($link);
?>
</html>
Abdo Adel
  • 1,177
  • 2
  • 17
  • 30
0

just use this in your tag in HTML page....

 meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

use < before meta word

  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/79114/discussion-on-answer-by-nouman-tahir-send-and-retrive-arabic-data-from-mysql-dat). – Taryn May 29 '15 at 10:48