Reputation: 333
I have content stored in a mysql database. When I am trying to retrieve the content using PHP and display it using html and CSS, some of it displays these special characters ������.
I have declared the encoding type in my HTML, used to render the PHP content as follows.
I have also added this to the head off the html document
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta charset="utf-8">
<!-- [portable options] -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0;" />
<meta name="apple-mobile-web-app-capable" content="yes" />
What mistake am I making that the retrieved content shows the special characters?
This is the document that contains the PHP script that retrieves the data from mysql
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta charset="utf-8">
<!-- [loading stylesheets] -->
<link type="text/css" rel="stylesheet" href="css/style.css" />
<!-- [loading stylesheets] -->
<link type="text/css" rel="stylesheet" href="css/style.css" />
<link type="text/css" rel="stylesheet" href="css/flexslider.css" />
<!-- [loading scripts] -->
<script type="text/javascript" src="js/jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</head>
<body>
<div class="blog-post">
<div class="wrapper">
<!--
<iframe frameborder="0" scrolling="yes" width="100%" height="500px"
src="debug/includes/news_index.php" name="" id="">
</iframe>
-->
<?php
// connection string constants
define('DSN', 'mysql:dbname=mydb;host=localhost');
define('USER', 'myadmin');
define('PASSWORD', 'mypwd2015');
// pdo instance creation
$pdo = new PDO(DSN, USER, PASSWORD);
// query preparation
$stmt = $pdo->query("
SELECT title, introtext, id, created, created_by, catid
FROM mytbl_items
");
// fetching results
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
// if this returns 0 then it means no records are present
echo count($result) . "\n";
// the loop should print valid table
foreach ($result as $index => $row) {
if ($index == 0) echo '<div>';
echo <<<HTM
<span class="post-date">{$row['created']}</span>
<h2 class="blog-post-title">{$row['title']}</h2>
<p>
{$row['introtext']}
</p>
<p>
<a href='read_serverside.php?id={$row['id']}'><input type="button" value="Read More" /></a>
</p>
<div class="blog-meta">
<img src="img/avatar.png" alt="Avatar" />
<h4 class="blog-meta-author">{$row['created_by']}</h4>
<span>Category: {$row['catid']}</span>
</div>
HTM;
if ($index == (count($result)-1)) echo '</div>';
}
?>
</div> <!-- .blog-meta -->
</div> <!-- .blog-post #2 -->
</body>
</html>
What else should I do to avoid the special chars from showing up?
Upvotes: 3
Views: 3213
Reputation: 522
This works for me. Try this one before the start of HTML. I hope it will also work for you.
<?php header('Content-Type: text/html; charset=iso-8859-15'); ?>
<!DOCTYPE html>
<html lang="en-US">
<head>
Upvotes: 1
Reputation: 723
Try setting the utf8 charset at the PDO Connection like this:
$pdo = new PDO(DSN, USER, PASSWORD,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
Upvotes: 1
Reputation: 17289
If you have main or front controller you should put
header('Content-Type: text/html; charset=utf-8');
somewhere there. If you haven't put that call at the very beginning of your file before you start output html:
<?php header('Content-Type: text/html; charset=utf-8'); ?>
<!DOCTYPE html>
<html>
<head>
......
Upvotes: 0
Reputation: 95
There are characters in your database that does not exist in Unicode UTF 8. The data are not recognized by this format so thew are displayed as weird characters. I suggest you change the <meta charset="utf-8">
into @charset 'iso-8859-15'
or to delete the line. If this does not work you have to go to the data that are causing the error and determine the type of encoding that is used.
Upvotes: 0