0

I tried many type of solution like:

-htmlentities -> When using this, all words have ñ will not display

-htmlspecialchars -> all words have ñ will be "?"

-utf8_encode ->all words have ñ will be "?"

-html_entity_decode(htmlentities($test)); ->When using this, all words have ñ will not display

my code what just a simple select, this is my code:

if (isset($_GET['cityname1'])) 
{

$cityname = strval($_GET['cityname1']);
$cname = mysql_query("SELECT cityname FROM city WHERE provinceid = '$cityname' ORDER BY cityname ASC");

echo "<option value='0'>Select Territory</option>";

  while($provincerow = mysql_fetch_array($cname))
  {
  $pname = htmlentities($provincerow['cityname']);
  echo "<option value='{$pname}'>{pname}</option>";
  }
}
else
{
echo "Please Contact the technical team";
}
shiro Jacinto
  • 155
  • 1
  • 12
  • Sanitising is normally for inserting data, not selecting and presenting it - just use `$provincerow['cityname']` – scrowler Aug 11 '15 at 01:59
  • sir i try echo $provincerow['cityname'], but the result is "Las pi?as"... the ñ just convert to ?(question mark) with black background – shiro Jacinto Aug 11 '15 at 02:01
  • You will probably need to change the encoding on your database then – scrowler Aug 11 '15 at 02:02
  • in that code all the word with ñ are not display, it was just simple select ?..... just displaying the output in dropdown box.. :( – shiro Jacinto Aug 11 '15 at 02:06
  • can somebody try... you just have a value or words with ñ stored in database, then fetch all the value and display it in dropdown :( thanks for the help in advance and sorry for my newbie question – shiro Jacinto Aug 11 '15 at 02:13
  • 1
    Make sure the charset in everywhere (browser, html, database, connection, etc.) are the same. – Tan Hong Tat Aug 11 '15 at 02:22
  • i set also the latin_swedish_ci to utf8_swedish_ci... is this ok? because some forum say they solve their problem.. but in my problem it was just null value using htmlentities, Las Pi?as value using utf8_decode, Las Pi�as value using html_entity_decode, null value using htmlspecialchars – shiro Jacinto Aug 11 '15 at 02:32
  • Thanks the help of other. I just solve my problem by adding to my sql mysql_query("SET NAMES 'utf8'", $db); mysql_query("SET CHARACTER_SET 'utf8'", $db)... thanks all of you sir – shiro Jacinto Aug 11 '15 at 02:44
  • Don't use the `mysql_*` interface, it is going away soon. Switch to `mysqli_*` or PDO. – Rick James Aug 28 '15 at 04:36
  • Do `SELECT col, HEX(col) FROM ... WHERE ...;` If you don't see `C3B1` for the hex for `ñ`, then you have problems in the `INSERT` side. – Rick James Aug 28 '15 at 04:39
  • The black diamond (�) is the browser's way of saying wtf. – Rick James Aug 28 '15 at 04:39

1 Answers1

1

Ahh, you have stumbled into the wondrous world of character encodings in browsers, PHP and MySQL.

Handling these characters is not something trivial since it is dependent on a number of factors. Normally speaking communication between PHP and MySQL is not in UTF-8 meaning that special characters (like ñ) get jumbled. So setting the connection to UTF-8 is a good start. Furthermore, it can be the case that PHP is not operating in a UTF-8 compliant manner, which can be checked (and set) using the function described here.

When this settings have been set correctely, you should be able to use the html_entities function to properly replace the character to the HTML character encoding (ñ).

The main problem with communcation between different services (like PHP and MySQL) is that when they are not using the same character encoding, characters (which are basically numbers) will be jumbled. Since both MySQL and PHP would be using different numbers for a certain character. For non special characters (like the non-accented alphabet) this works out, since these are extremely standardised, yet for more odd characters there still is some struggle as you have experienced.

Note that this answer assumes a basic setup, if I have made an unjustified assumption, please provide feedback, then I can help you further.

  • sir thank you for your concern. :) i just solve my problem :) just adding mysql_query("SET NAMES 'utf8'", $db); mysql_query("SET CHARACTER_SET 'utf8'", $db in my mysql connection :) http://community.sitepoint.com/t/php-mysql-and-latin-characters-aeiou-and-n-problem/3684/7 – shiro Jacinto Aug 11 '15 at 02:46