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";
}
11
  • Sanitising is normally for inserting data, not selecting and presenting it - just use $provincerow['cityname'] Commented Aug 11, 2015 at 1:59
  • sir i try echo $provincerow['cityname'], but the result is "Las pi?as"... the ñ just convert to ?(question mark) with black background Commented Aug 11, 2015 at 2:01
  • You will probably need to change the encoding on your database then Commented Aug 11, 2015 at 2:02
  • in that code all the word with ñ are not display, it was just simple select ?..... just displaying the output in dropdown box.. :( Commented Aug 11, 2015 at 2:06
  • 1
    Make sure the charset in everywhere (browser, html, database, connection, etc.) are the same. Commented Aug 11, 2015 at 2:22

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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 :) community.sitepoint.com/t/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.