PHP - Display characters in Arabic? - php

The problem is the display of the Arabic language in php, with French and English it works well but in Arabic it only displays the: ???
this is a code
`
// connexion
$host="****";
$user="****";
$password="****";
$dbname="****";
$con=mysqli_connect($host,$user,$password,$dbname);
$lang = trim($_GET['lang']);
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES 'utf8'");
$sql="SELECT title,image,rating,releaseYear,id,pays,language FROM user WHERE language='$lang' and pays='$rtt ' ORDER BY `user`.`id` DESC" ;
$result=mysqli_query($con,$sql);
if($result)
{
while($row=mysqli_fetch_array($result))
{ $flag[]=array('title'=>utf8_encode($row['title']),'image'=>$row['image'],'rating'=>utf8_encode($row['rating']),'releaseYear'=>$row['releaseYear'],'id'=>$row['id']);
}
print_r(json_encode($flag, JSON_UNESCAPED_UNICODE));
}
mysqli_close($con);
?>`
this is a image enter image description here

Did you verify your browser charset ?
<head>
<meta charset="utf-8">
</head>

Related

Php mysql returning?

I have a problem, when I try to echo a cyrillic character, it return like ????
Here's code
<?
include('db.php');
$sql = "SELECT * FROM menu_items WHERE reference=1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$rows = array();
while($row = $result->fetch_object()) {
$rows[] = json_encode($row);
}
$items = implode(',',$rows);
echo '['.$items.']';
}else {
echo "ERROR";
}
?>
Any idea?
Collation : utf8_general_ci
And db.php:
<?
$servername = "localhost";
$username = "test";
$password = "Conqwe333!";
$conn=mysqli_connect($servername,$username,$password,"test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Worked after <? $conn->set_charset("utf8");?>
Add before your $sql
$conn->query('SET NAMES utf8');
You can read more about it here
Also you will need to set proper header for browser. You can do it by serveral ways for example in meta html tag or using header('Content-Type: text/html; charset=utf-8');
You should set collation per connection:
mysqli_set_charset
Also you can perform sql
SET NAMES utf8;
but it's not recommended
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* change character set to utf8 */
if (!$mysqli->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
printf("Current character set: %s\n", $mysqli->character_set_name());
}
$mysqli->close();
I am assuming you are using Bulgarian and UTF8, same will work for Russian and other languages, just change "bg" to proper string.
I do not recommend you to use cp1251, because it breaks unexpectedly with apache mod_rewrite and other tools like this.
You need to do following checks:
Check if your database / table collation is some UTF8. It could be utf8_general_ci or Bulgarian - difference is minimal and is more sorting related. (utf8_general_ci is perfectly OK)
Check you have following statement executed right after connect - set names UTF8;. You can do $mysqli->query("set names utf8");
Make sure you have proper "tags". Here an example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='bg' xml:lang='bg' xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Нов сайт :)</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
You can include UTF8 "BOM" on the html, but it works pretty well without it. I usually work without "BOM", and when I want to be 100% complaint, I create an include file bom.php that contain just the BOM symbol and include it prior HTML template in normal PHP way, e.g. include "bom.php".
Hope this helps, if not, please comment.
EDIT:
Someone suggested you must be sure if your data is properly stored in MySQL. Easiest way is to open PHP MySQL Admin. If Cyrillic is shown there, all is OK.
I think the issue is a step back, try to first encode the cyrillic characters correctly: How to encode cyrillic in mysql?

Store Arabic text in mysql database using php [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 1 year ago.
I am trying to store some Arabic data in a mysql database
I have set the html document charset to be 'utf8'
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
MySQL charset is set to: UTF-8 Unicode (utf8)
MySQL connection collation is set to: utf8_general_ci
Database and table collations are set to: utf8_general_ci
In addition in my php code I used the $mysqli->set_charset("utf8") function to ensure that the charset is set to be ut8 but nothing is actually working , I am posting my data to the php script using html form where the enctype is : enctype="multipart/form-data" because in this form I also upload an image
The weird thing is that when I write my query directly in mysql the Arabic characters are stored properly with no problem , but when I use a php query to store the data all the characters are stored in a wrong charset encoding
Any suggestions ?
Weird, It should work!
Try adding these lines after a successful connection to the database:
$mysqli->query("SET NAMES 'utf8'");
$mysqli->query("SET CHARACTER SET utf8");
For example:
$mysqli = #new mysqli('DB_HOST', 'DB_USER', 'DB_PASS', 'DB_NAME');
if($mysqli->connect_errno) die('Connection Error!');
else{
$mysqli->query("SET NAMES 'utf8'");
$mysqli->query("SET CHARACTER SET utf8");
}
And include these META tags in the head section of your php page:
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Browsers will ignore the <meta http-equiv ...> tag if there is a http header present. Try putting this at the top of your php page:
header("Content-Type: text/html; charset=UTF-8");
save this file in ur website, htdocs folder as "dbh.php, then in include it at the top of your index page by typing <?php include 'dbh.php';?> and you u are ready to go.
<?php
$conn = mysqli_connect("localhost", "root", "", "database name");
if(!$conn){
die("connection failed: ".mysqli_connect_error());
}else{
$conn->query("SET NAMES 'utf8'");
$conn->query("SET CHARACTER SET utf8");
}
You may try this. It should work
<?php
$conn = mysqli_connect(<host>, <user name>, <password>, <database name>);
if(!$conn){
die("could not connect: ".mysqli_connect_error());
}else{
mysqli_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn);
}
?>

Displaying Japanese characters from MySql database on PHP page

I'm trying to make a page that displays japanese characters. These japanese characters are stored in my database. I've done my research and tried many of the suggestions given but my japanese characters still return as question marks. Currently, this is how my code looks:
<?php
$con=mysqli_connect("host", "user", "pw", "db");
if (!$con)
{
die('Failed to connect to mySQL: ' .mysqli_connect_errno());
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php
mb_internal_encoding('UTF-8'); ?>
<center>
<div id="fb-root"></div>
<form method="POST" id="myForm">
<h1>
<?php
$title = mysqli_query($con, "SELECT `title` FROM titles WHERE c1='a' or c2 ='b'") or die("ERROR: ". mysqli_error($con));
$title_str = mysqli_fetch_array($title);
$mystr = mb_convert_encoding($title_str[0], "UTF-8");
echo "test: ".$mystr;
?>
</h1><br/>
</form>
</body>
I've already checked if my character encoding works and it does work if I replace $title_str[0] to japanese string. Am I missing something here? I'm not sure why the encoding works on strings I manually input but not the one I got from the database.
Try using SET NAMES utf8 after connecting to MySQL:
$con=mysqli_connect("host", "user", "pw", "db");
if (!$con)
{
die('Failed to connect to mySQL: ' .mysqli_connect_errno());
}
mysqli_query($con, "SET NAMES 'UTF8'") or die("ERROR: ". mysqli_error($con));
As the manual says:
SET NAMES indicates what character set the client will use to send SQL
statements to the server... It also specifies the character set that the server should
use for sending results back to the client.
After that there is no need to use mb_convert_encoding.

php echo page title in foreign characters

I want to echo page title from database value.
<title><?php echo $data['art_title'] ?></title>
But that language is Chinese. Title is not showing correctly. It's look like this now.
how to fix this problem? thank you.
When you saving your page choose its endoding utf-8 or another chinese supported encoding.
If you already are doing things right (writing both string in UTF-8) the problem can be Chrome/Firefox tryiing to use a font that is not unicode aware for the titles (?)... if thats the case, theres nothing you can do (report it?)
Just in case, make sure your page is correctly encoded (text in utf8, with the page declared in utf8).
UPDATE:
You could be hitting a OS/Browser bug:
http://code.google.com/p/chromium/issues/detail?id=90752
I found a solution.
$sql = 'SET CHARACTER SET utf8';
here is full code
<?php
// MySQl connection
$db = mysql_connect('host', 'user', 'password');
// Select the database
mysql_select_db('db',$db);
$sql = 'SET CHARACTER SET utf8';
$result = mysql_query($sql, $db);
// SQL query
$sql = "SELECT art_title, art_meta FROM article WHERE art_id=".$_GET['art_id'];
// Send the query
$req = mysql_query($sql) or die('Error SQL !<br>'.$sql.'<br>'.mysql_error());
$data = mysql_fetch_assoc($req);
?>
<meta name="keywords" content="<?php echo $data['art_meta']; ?>" />
<title><?php echo $data['art_title']; ?></title>

MySQL database query coding problem in PHP

Hello I have this problem: I have MySQL Database with collation latin1_bin. In database I have table with collation utf8_slovak_ci. All collumns is text. In rows I have values with ť í ž ľ š č(utf8) chars, but PHP code returns values with utf8 chars replaced by � , ? and other not readable chars. In html head I have set charset to utf-8. Please help me. I have this code:
//From config file
define ("DATABASE_SERVER", "localhost");
define ("DATABASE_LOGIN", "root");
define ("DATABASE_PASSWORD", "root");
define ("DATABASE_DATABASE", "novymost");
mysql_connect(DATABASE_SERVER, DATABASE_LOGIN, DATABASE_PASSWORD)or die(mysql_error());
mysql_select_db("novymost")or die(mysql_error());
$results=mysql_query("SELECT * FROM Downloads") or die(mysql_error());
$row = mysql_fetch_array( $result );
// Print out the contents of the entry
while($rows = mysql_fetch_array($results))
{
echo("<li>");
echo "<a href=\"".$rows["URL"];
echo("\">");
echo "".$rows["TITLE"];
echo(" - ".$rows["SIZE"]);
echo("</a>");
echo("<br/></li>");
}
run the query
mysql_query( "set names utf8" );
before runnign any other queries. This sets the connection charset
http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
In php:
header("Content-Type: text/html; charset=UTF-8");
In HTML:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
In Database before queries:
mysql_query( "set names utf8" );
Or you can also convert you latin-table to a UTF8-table:
INSERT INTO utf8table (utf8column)
SELECT CONVERT(latin1field USING utf8)
FROM latin1table;

Categories