MySQL encoding utf8_general_ci - php

I've got a database with utf8_general_ci encoding, but when I fetch the data with php to the page it shows "????", what's wrong?
<?php
$query1 = "SELECT name FROM `kotegorii`";
$result1 = mysql_query($query1) or die(mysql_error());
echo "<h3>Категориялар</h3>";
while ($row = mysql_fetch_array($result1)) {
echo $row['name']."<br>";
}
?>
the code is written using the UTF-8 encoding also..

You have to set the MySQL connection encoding to UTF-8 right after connecting to the database with mysql_set_charset:
mysql_set_charset('utf8');
The server uses the connection encoding to send information to the client, no matter how the text is encoded in the storage. The connection encoding is latin1 by default on many installations.
Also, you shouldn't use the old mysql API in new code; it is deprecated and will be removed from PHP in some future version.

You should set page encoding too.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<!-- and so on -->

I don't know how your data looks but have you tried utf8_decode utf8_encode on $row['name']?

Related

Html charset for German char not working

I have set in my html page:
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
but still I am not able to see the special chars and instead there is replacement.
I have set:
define("MENU_TEXT_LINK3", "Medica Ärztebedarf");
and as you can see in the top right menu site it is not shown.
The meta charset in html page was set fine. The problem was that the packet header was not set fine, so I changed basic configuraation charset in codeigniter and it works
You can try change the file charset encoding or use html entities to avoid that.

chinese character issue in file_get_contents

i was trying very basic file get contents for chinese character and for most of the sites it works if charset is specified as utf8 but for few sites it gives something messy. here is my code and putput. any observation/suggestion is welcome
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head> <?php
$homepage = file_get_contents('http://wallpaper.pconline.com.cn/pic/23731.html');
echo $homepage;
?>
i have checked in phpfiddle also there also same output http://phpfiddle.org/
���R�6(A�j �rr ۖ���U�vu�n��#���SJsS6�Wk��Sd��ڸݰ��Sk��^�̩j��(F3%M(����3��;ʬ:�4ڪ�!-E��q����9��M{U���^�f���9�Z;���[o\;w��~���K��:y����>�s���˟�>u������� �_]�p����^�����S'�t��կ����e�0ˢ��fs�t����W/\������T

PHP Japanese echo string becomes question marks

I'm working on a php site that needs both Japanese and English. In most places I have no trouble getting the Japanese to display correctly, but I can't get the characters to display if I use a function like this:
echo.php:
<?php
function draw(){
echo "日本語";
}
draw();
?>
I get "日本語"
but if I try this :
index.php:
<?php
some stuff
include "echo.php";
draw();
?>
I get "???".
Is there any way for me to get my echo file to send the data to the first file in a way it can read it?
EDIT:
The website works, and shows Japanese characters correctly, except when it tries to pull a function from a php file
Here's the code:
<html lang="ja">
<head>
<title>Running Projects</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body>
<div id="header">
<? php include "layout.php" ?>
</div>
</body>
</html>
Where layout.php is just a file with a list of links, I have this on each page so the links are on every page.
For a different part, I have to use functions.php to get some data from a database and write it to the page, so, I tried putting layout.php into the functions.php and calling it: The English links appeared, but the Japanese links appeared as question marks.
You should change your file encoding to UTF-8 and set the header on the website to UTF-8.
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
in HTML5 you should use:
<meta charset="utf-8" />
or in php
header('content-type: text/html; charset=utf-8');
You problem definitely has something to do with the character encoding. Try to check the following:
All your strings in all PHP scripts are Unicode (UTF-8 is a very common choice). Use utf8_encode() and/or utf8_decode() to force UTF-8 on your strings where necessary.
Your server sends PHP output as Unicode (UTF-8 and preferably, but not necessarily, gzipped data)
Your browser understands and accepts Unicode (UTF-8). Typically browser would send Accept-Charset: UTF-8,*;q=0.5 in the GET request to hint it's Unicode capability.
Finally, I have checked your code with PHP version 5.3.6 and Firefox, Chrome and IE 9. Firefox and Chrome prints the Japanese characters as expected. It's only IE 9 which doesn't print it correctly. Snooping on the GET request from IE reveals, it is indeed not sending any Accept-Charset: UTF-8,*;q=0.5. I am not entirely sure how to force IE to send that because in my case, clearly my server (Apache) together with PHP was definitely sending UTF-8 string back.
One more hint - you may try converting your strings into HTML entities. For example - echo "©"; would print ©. But, I am not 100% sure how to convert all strings into HTML entities using PHP. I have unsuccessfully attempted with htmlentities() and htmlspecialchars(). But, it didn't change anything for IE.
i have same problem. But i handle it.
convert "echo.php"'s encoding to EUC-JP or SHIFT-JIS.
<?php
function draw(){
echo mb_convert_encoding("日本語", 'UTF-8', array('EUC-JP', 'SHIFT-JIS', 'AUTO'));
}
draw();
?>
This works for me :)
That happens when charset is not defined or is incorrect.
You can use meta tags to define the charsets. Place the following meta tags as needed on the head section of the page, and It will be rendered correctly.
HTML 4
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
HTML 5
<meta charset="utf-8" />
add below meta tags to header
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta http-equiv="Content-Language" content="ja" />
MySQL and PHP need to be handling the same character set.
Try adding ..
mysqli_set_charset("utf8");
.. after connecting to your database. Always works for me!
If you are using php and mssql you will also need to specify characterset in the mssql connection string.
Like this:
$connectionInfo = array("Database"=>"db name", "UID"=>"username", "PWD"=>"pw","CharacterSet" => "UTF-8");
$serverName = "server";
$conn = sqlsrv_connect($serverName, $connectionInfo);
Please note you still need to do the things suggested above
declare your html charset like this HTML 5:
<meta charset="utf-8" />
php charset like this:
header('content-type: text/html; charset=utf-8');

UTF-8 not working outside the header

Utf-8 works just for text generated in header, but not for text in (for example) index. Does anyone have an idea how to fix that?
header.php:
<?php
header('Content-Type: text/html; charset=utf-8');
?>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
....
for example: index.php
<?php
include "header.php";
?>
...body stuff
Thanks :)
Check your file encoding. I don't know what editor you usually use, but in Eclipse you can have a look to in via right click on a file -> properties
For database, check the encoding of each fields & be sure to call "set names utf8" query each time you open a mysql connexion

php page not displaying arabic text

I have a PHP page where I am trying to display Arabic text but its showing.
In MySQL database I'm storing arabic text successfully.
I'm using the following code to connect to the database :
function connect(){
$this->dbLink = mysql_connect($this->dbHost,$this->dbUser,$this->dbPass);
if(!$this->dbLink) die("Could not connect to database. " . mysql_error());
mysql_select_db($this->dbName);
mysql_set_charset("utf8", $this->dbLink);
}
And using the following header in the PHP page :
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
But still no success.
Thanks for helping.
You can use this meta code:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
And please check mysql column's character type..
Look to this
check the file encoding, it should be UTF-8, and you can try to run the following query before querying the text:
mysql_query("SET NAMES UTF8");
Addtional to the headers set in the meta section, check your default_charset setting in the php.ini. default_charset should be empty if you set content type header by your own or correspond with your content-type meta information.
You set a META-tag that tells the browser to use ISO-8859-1 (<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />), which is for latin-based scripts, but surely not Arabic.
Also make sure you have UTF-8 at all places:
the DB-column
the DB-connection (you already have it)
the website (via HTTP-header OR META-tag, you don't need both, and you don't need a second META-tag, as stated above…)
Have you tried setting the connection to use UTF-8 (with mysql_set_charset)?
Try to add this two lines to your php script after the Mysql connection
mysql_query("SET NAMES cp1256");
mysql_query("set character set cp1256");

Categories