Jump to content

Problem For View Special Chars From Mysql On Php


aganabar

Recommended Posts

Hi!

 

I'm a spanish user of your free hosting.

 

My problem is that i can't view on my website spanish specials chars, like á, ñ, é...

 

I have uft8 charset, utf8_spanish_ci collate and i can see the special chars on my website, but not from mysql. What is my problem?

 

Thanks!!

Link to comment
Share on other sites

Hi there,

 

I'm not staff but I think I might be able to help you. :)

 

Database

While it's probably better to be consistent, database collations don't really matter. Just tested that.

 

PHP

Let's make sure PHP is ready to display unicode characters.

 

Tell your database connection to use utf8 after connecting:

<?php

// Establish database connection
$db = new mysqli($dbHost, $dbUser, $dbPassword, $dbName);
$db->set_charset('utf8');

// Catch any connection errors
if ($db->connect_errno) {
   die('DB Error: ' . $db->connect_error);
}

 

Escape database data before outputting to a web page:

<?php

// Require database connection file
require_once 'database.php';

// Create escape function
function e($string) {
   return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
}

// Getting data from database
$data = $db->query("SELECT * FROM guestbook");

// Escape and output to page
while ($row = $data->fetch_assoc()) {
   echo e($row['message']) . '<br>';
}

 

HTML

Define your charset in the head of your HTML:

<meta charset="utf-8">

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...