Problems with Ñ in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
totoylubacon
Forum Newbie
Posts: 7
Joined: Thu Mar 01, 2012 5:48 am

Problems with Ñ in PHP

Post by totoylubacon »

Please help!

I am developing a website, but i encountered problem in saving to MySQL database the character Ñ, when I tried to save the LastName DE LA PEÑA, it stores to a database like this DE LA PEÃ?, when i tried to view in a page it looks like this DE LA PEÃ?A , anybody who has an idea on how to solve this problem, below is my source code:

mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET 'utf8'");
$sqlUpdateReg = "UPDATE registration set REG_ID = '$getID',FNAME = '$FirstName',MNAME = '$MiddleName',LNAME = '$LastName',COURSE_NAME='$Course',
YEAR_LEVEL = '$yearlevel',DATE_ENROLL='$dateEnroll',ENROLLMENT_STATUS='$enrollstatus' WHERE REG_ID = $getID";

$resultUpdate = @mysql_query($sqlUpdateReg) or die("Updating of Student Records is failed".mysql_error());

And i also include the code <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head>

Please help.
Thanks
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Problems with Ñ in PHP

Post by social_experiment »

Try changing the charset to iso-8859-1 or have a look at this url
http://htmlhelp.com/reference/html40/en ... atin1.html
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
xtiano77
Forum Commoner
Posts: 72
Joined: Tue Sep 22, 2009 10:53 am
Location: Texas

Re: Problems with Ñ in PHP

Post by xtiano77 »

You can use the “htmlentities( )” & the “html_entity_decode( )” prior to adding the value to the SQL string or before echoing the SQL statement result to the user. You can also use the “str_replace( )” with arrays specifying targets and replacements.
totoylubacon
Forum Newbie
Posts: 7
Joined: Thu Mar 01, 2012 5:48 am

Re: Problems with Ñ in PHP

Post by totoylubacon »

I tried using str_replace, i don't know where what is my error, but it store to database like this DE LA PE&ATILDE;?A, and display to the page as DE LA PE&ATILDE;?A

here is my code:

$inye = array("Ñ","ñ");
$lname = str_replace($inye,'&Ntilde;',$LastName);

the value of $LastName = DE LA PEÑA
totoylubacon
Forum Newbie
Posts: 7
Joined: Thu Mar 01, 2012 5:48 am

Re: Problems with Ñ in PHP

Post by totoylubacon »

Thank you xtiano77 for the idea you have shared, i got it already, it works fine.

For those who have same problem as mine.

Here is my code it works fine for me.

//database connection, i google this
<?php

$conn = mysql_connect('localhost','user','password')
or die(mysql_error());
mysql_select_db('database', $conn) or die(mysql_error());
mysql_query("SET NAMES 'utf8'", $conn);
mysql_query("SET CHARACTER_SET 'utf8'", $conn);
?>

//form to process inputs code

$lname =mysql_real_escape_string(htmlentities(trim($_POST['LastName'])));
$lastname = html_entity_decode($lname);

....code to insert into mysql database...

//add this script between <head></head> tag
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />

Thanks!
kon
Forum Newbie
Posts: 19
Joined: Sat Mar 03, 2012 5:43 am

Re: Problems with Ñ in PHP

Post by kon »

Actually you shouldn’t need htmlentities at all. Maybe this is something worth searching a bit more because you will facing it a lot (since you write for other languages than English). What is the form method post or get? Also what is the enctype ?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Problems with Ñ in PHP

Post by social_experiment »

After testing this i am inclined to agree with kon, i entered the value (Ñ) and set the collation of the field first to utf8-bin then utf8-spanish_ci and even without using htmlentities (or even a doctype header) i am able to display the value correctly. How do you enter the value; copy/paste or do you use alt+0209?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
totoylubacon
Forum Newbie
Posts: 7
Joined: Thu Mar 01, 2012 5:48 am

Re: Problems with Ñ in PHP

Post by totoylubacon »

I agree with you both kon and social_experiment, i tried also to exclude the htmlentities, it works fine for me. Anyway I just added the htmlentities just to clean the user-input from the form, the form method is get. Thanks for the nice suggestion.
Post Reply