Najah Mousa
Najah Mousa

Reputation: 23

send and retrive arabic data from mysql database

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);
?>

Upvotes: 2

Views: 6152

Answers (4)

rs_
rs_

Reputation: 93

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

Upvotes: 2

Rick James
Rick James

Reputation: 142296

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.

Upvotes: 1

Abdelrhman Adel
Abdelrhman Adel

Reputation: 1187

  • 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>

Upvotes: 0

Nouman Tahir
Nouman Tahir

Reputation: 1

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

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

use < before meta word

Upvotes: 0

Related Questions