Asked  7 Months ago    Answers:  5   Viewed   50 times

I have in the database words that include special character (in Spanish mostly, like tildes). In the database everything is saved and shown correctly with PHPmyAdmin, but when I get the data (using PHP) and display it in a browser, I get a weird character, like a "?" with a square... I need a general fix so I don't need to escape each character every time, and also I would be able to insert special Spanish characters from a PHP form into the database...

The HTML is correct:

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

All tables and databas are set to utf8_spanish

The character I get: ?

Any suggestions???




Changed the HTML charset to ISO-8859-1 fixed the problem! Silly

Wednesday, March 31, 2021
answered 7 Months ago

Your sql string will be:

INSERT INTO `table` (`row1`) VALUES ('google's site')

Which is not a valid statement. As Nanne wrote, escape the string at least with mysql_real_escape_string :

And read about sql injection

Think a bit: if someone posts this: $_POST['text'] with value: ');delete from table;....

Your can say good bye to your data :)

Always filter/escape input!

EDIT: As of PHP 5.5.0 mysql_real_escape_string and the mysql extension are deprecated. Please use mysqli extension and mysqli::escape_string function instead

Wednesday, March 31, 2021
answered 7 Months ago

You need to have everything in utf-8:

  • The database field
  • The database connection (mysql_set_charset('utf8'); in classic mysql, something like $db->exec('SET CHARACTER SET utf8'); in PDO)
  • The content type (like you have already)
Wednesday, March 31, 2021
answered 7 Months ago
  1. Make sure you set the headers to UTF-8:

    header('Content-Type: application/json; charset=utf-8');
  2. Make sure your connection to database is made with UTF-8 encoding before any queries:

    $query = mysql_query("SET NAMES 'UTF8'");
  3. As far as I know, JSON encodes any characters that cannot be represented in pure ASCII. And you should decode that JSON on response.

  4. Try to move to PDO as mysql_* functions are deprecated. Use this nice tutorial

Saturday, May 29, 2021
answered 5 Months ago

You should change the charset/collation of your table. You have a table which stores its data in ASCII format when you try to filter it with UNICODE (UTF8) strings.

Have a look at this documentation to understand the matter.

To change the charset of a table :

Saturday, May 29, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :