php echo page title in foreign characters - php

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>

Related

Utf8 encoding mysql query not working

I have a problem with a mysql query. This is the code:
session_start ();
$db = mysql_connect('localhost', 'user', 'pw') or die(mysql_error());
mysql_select_db("wordpress_97") or die(mysql_error());
$names=mysql_query('set names utf8');
$tempnewpw = md5($tempnewpw);
$query = mysql_query("UPDATE wordpress_97.tUsers SET passwort = '$tempnewpw' WHERE tUsers.UserID = '$theuserid'") or die(mysql_error());
Yes I know that I shouldn't use the mysql_ functions but it's just testing..
And this is the error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE wordpress_97.tUsers SET passwort = 'pwinmd5'' at line 1
I can't get away these Â...
The database shouldn't be the problem - other querys work without problems with the same structure.
Any ideas?
Thanks in advance
Delete the line and re-type it.
Most likely those are characters carried over from copy-pasting.
Regarding your second question, d41d8cd98f00b204e9800998ecf8427e is the md5 hash of null or nothing, a zero-length stream of characters.
On this line
$tempnewpw = md5($tempnewpw);
Make sure that $tempnewpw exists and has a value, I should suggest also that you use different variables instead of over-writing them.
Try this use mysql_set_charset
session_start (); # not use for the DB query's
$db = mysql_connect('localhost', 'user', 'pw') or die(mysql_error());
mysql_set_charset('utf8',$db); # add this
$db_selected = mysql_select_db("wordpress_97",$db);
//$names=mysql_query('set names utf8'); --- remove
if (!$db_selected) {
die ('Database access error : ' . mysql_error());
}
else{
$tempnewpw = md5($tempnewpw);
$query = mysql_query("UPDATE wordpress_97.tUsers SET passwort = '$tempnewpw' WHERE tUsers.UserID = '$theuserid'")
or die(mysql_error());
}
In html
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
you can try to add this line in your html head tag:
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
Don't use Word for editing. It introduces funny characters such as hex A0, which through some conversions becomes  (A-hat and a space), as seen in your text.
I hope you don't have any accented letters; I suspect they have been mangled by other things -- such as failure to be consistent in CHARACTER SET.

I can't get Chinese characters from MYSQL to PHP

I found a lot of answers for my problem but nothing worked.
I have Chinese characters in my MYSQL database and when I SELECT my field in php I get '???' instead of '我们的产品'. I was able to INSERT this characters from php to my MYSQL database.
With characters like 'éà' I have no problem to get them in PHP.
My database, table and field are encoded in utf8_unicode_ci. There is my php file(also encoded in utf-8) :
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<?php
$query = mysql_query("SELECT * FROM table");
$arr = mysql_fetch_array($query))
echo ($arr['field']);
?>
If I replace my code like this it prints 我们的产品 :
$query = mysql_query("SELECT '我们的产品'");
arr = mysql_fetch_array($query)
echo ($arr[0]);
I hope I gave enough information.
set charset as utf8 before making a mysql query, like this
mysql_connect('localhost','mysql_user','your_password');
mysql_select_db('my_db') or die( "Unable to select database");
mysql_set_charset('UTF8');
mysql_query('Your query here');

sql data charset / charactter set , set names not working

i have the mysql connection below:
$dbLink = mysql_connect('127.0.0.1', 'root', '');
mysql_set_charset('utf8', $dbLink);
mysql_select_db('test', $dbLink);
mysql_set_charset('utf-8');
it was working fine 1 week ago, but suddenly it is not showing the utf-8 or arabic or unicode contents which are stored in the database. but now even in the database the characters has been changed like سید احمد Ø´Ûید Ú©ÛŒ صحیح and showing in the php the same. before everything was perfect. and the content was showing properly.
even now when i make a new entry in the database its showing fine but something went wrong with the old entries any help please.......
i have tried set names, mysql_set_charset & <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> but nothing happened. please help otherwise my site will be hanged up......
Use MYSQLi because MYSQL is now deprecated.
To set charset utf-8 use mysqli_set_charset() like this:
$dbLink = mysqli_connect('127.0.0.1', 'root', '');
mysqli_set_charset($dbLink , 'utf8');
mysqli_select_db($dbLink, 'test');
If for any reason it is still displaying wrong characters, add this to the beginning of your documents:
// Tell PHP that we're using UTF-8 strings until the end of the script
mb_internal_encoding('UTF-8');
// Tell PHP that we'll be outputting UTF-8 to the browser
mb_http_output('UTF-8');
UPDATE
Select some contents from your database and output the value using iconv() and see if it shows the wright charset:
<?php
$dbLink = mysqli_connect('127.0.0.1', 'root', '');
mysqli_set_charset($dbLink , 'utf8');
mysqli_select_db($dbLink, 'test');
function get_table_contents(){
global $dbLink;
$contents = array();
$query = "SELECT * FROM ||YOUR TABLE HERE||";
$result = mysqli_query($connection, $query);
while($content = mysqli_fetch_assoc($result)){
$contents[] = $content;
}
return $contents;
}
$data = get_table_contents();
?>
<html>
<head></head>
<body>
<?php echo iconv(mb_detect_encoding($data[0]['||YOUR COLUMN||'], "UTF-8,ISO-8859-1"), "UTF-8", $data[0]['||YOUR COLUMN||']); ?>
</body>
</html>

PHP Charset for special characters

I am creating my website with HTML and PHP with a post function. In the post, I am using special special characters (Å, Á ...), but they appear as � on the screen. However, all of the HTML content works.
Any idea?
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
getPosts();
?>
And the Functions file:
<?php
include('connect.php');
function getPosts() {
$query = mysql_query("SELECT * FROM posts") or die(mysql_error());
while($post = mysql_fetch_assoc($query)) {
echo "<h2>" . $post['Title'] . " by " . $post['Author'] . "</h2>";
echo $post['Content'];
}
}
?>
Make sure your MySQL character set and collation (at least for this database/table/column) is utf8.
Also make sure that you set the connection charset correctly:
mysql_set_charset ( "utf8" );
This requires PHP 5.2.3 and MySQL 5.0.7. Also consider switching to MySQLi or PDO which usually handles this better. The obsolete mysql_* API has been deprecated in PHP 5.5
chances are your mySQL database table is not in the correct collation.
ALTER TABLE `posts` CHANGE `content` `content` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ;
do a change like this for your entire database, table, or cell.

Problems in inserting utf-8 string into database and then outputting it to web page

I am learning PHP programming, so I have setup testing database and try to do various things with it. So situation is like that:
Database collation is utf8_general_ci.
There is table "books" created by query
create table books
( isbn char(13) not null primary key,
author char(50),
title char(100),
price float(4,2)
);
Then it is filled with some sample data - note that text entries are in russian. This query is saved as utf-8 without BOM .sql and executed.
insert into books values
("5-8459-0046-8", "Майкл Морган", "Java 2. Руководство разработчика", 34.99),
("5-8459-1082-X", "Кристофер Негус", "Linux. Библия пользователя", 24.99),
("5-8459-1134-6", "Марина Смолина", "CorelDRAW X3. Самоучитель", 24.99),
("5-8459-0426-9", "Родерик Смит", "Сетевые средства Linux", 49.99);
When I review contents of created table via phpMyAdmin, I get correct results.
When I retrieve data from this table and try to display it via php, I get question marks instead of russian symbols. Here is piece of my php code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Books</title>
</head>
<body>
<?php
header("Content-type: text/html; charset=utf-8");
mysqli_set_charset('utf8');
# $db = new mysqli('localhost', 'login', 'password', 'database');
$query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
$result = $db->query($query);
$num_results = $result->num_rows;
for ($i = 0; $i < $num_results; $i++) {
$row = $result->fetch_assoc();
echo "<p><strong>".($i+1).". Title: ";
echo htmlspecialchars (stripslashes($row['title']));
echo "</strong><br />Author: ";
echo stripslashes($row['author']);
echo "<br />ISBN: ";
echo stripslashes($row['isbn']);
echo "<br />Price: ";
echo stripslashes($row['price']);
echo "</p>";
}
...
And here is the output:
1. Название: Java 2. ??????????? ????????????
Автор: ????? ??????
ISBN: 5-8459-0046-8
Цена: 34.99
Can someone point out what I am doing wrong?
Can someone point out what I am doing wrong?
Yes, I can.
You didn't tell Mysql server, what data encoding you want.
Mysql can supply any encoding in case your page encoding is different from stored data encoding. And recode it on the fly.
Thus, it needs to be told of client's preferred encoding (your PHP code being that database client).
By default it's latin1. Thus, because there is no such symbols in the latin1 character table, question marks being returned instead.
There are 2 ways to tell mysql what encoding we want:
a slightly more preferred one is mysqli_set_charset() function (method in your case).
less preferred one is SET NAMES query.
But as long as you are using mysqli extension properly, doesn't really matter. (though you aren't)
Note that in mysql this encoding is called utf8, without dashes or spaces.
Try to set output charset:
SET NAMES 'utf-8'
SET CHARACTER SET utf-8
Create .htaccess file:
AddDefaultCharset utf-8
AddCharset utf-8 *
CharsetSourceEnc utf-8
CharsetDefault utf-8
Save files in UTF-8 without BOM.
Set charset in html head.
After your mysql_connect, set your connection to UTF-8 :
mysql_query("SET NAMES utf8");
Follow Alexander advices for .htaccess, header and files encoding
You probably need to call mysqli_set_charset('utf8'); after you set up your connection with new mysqli(...) as it works on a link rather than a global setting.
so..
# $db = new mysqli('localhost', 'login', 'password', 'database');
mysqli_set_charset($db, 'utf8');
$query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
By the way, that query seems to be open to SQL-injection unless $searchterm is sanitized. Just something to keep in mind, consider using prepared statements.
And using # to suppress errors is generally not recommended, especially not during development. Better to deal with error-conditions.
after your mysql_query add
#mysql_query("SET character_set_server='utf8'; ");
#mysql_query("SET character_set_client='utf8'; ");
#mysql_query("SET character_set_results='utf8'; ");
#mysql_query("SET character_set_connection='utf8'; ");
#mysql_query("SET character_set_database='utf8'; ");
#mysql_query("SET collation_connection='utf8_general_ci'; ");
#mysql_query("SET collation_database='utf8_general_ci'; ");
#mysql_query("SET collation_server='utf8_general_ci'; ");
Try to put also in the HTML document Head the meta tag:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
this is different to the HTTP header header("Content-type: text/html; charset=utf-8");

Categories