Page 1 of 1

Problems with Ñ in PHP

Posted: Thu Mar 01, 2012 6:06 am
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

Re: Problems with Ñ in PHP

Posted: Thu Mar 01, 2012 4:09 pm
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

Re: Problems with Ñ in PHP

Posted: Thu Mar 01, 2012 4:37 pm
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.

Re: Problems with Ñ in PHP

Posted: Fri Mar 02, 2012 8:41 am
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

Re: Problems with Ñ in PHP

Posted: Sat Mar 03, 2012 3:42 am
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!

Re: Problems with Ñ in PHP

Posted: Sat Mar 03, 2012 5:51 am
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 ?

Re: Problems with Ñ in PHP

Posted: Sun Mar 04, 2012 2:37 am
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?

Re: Problems with Ñ in PHP

Posted: Wed Mar 07, 2012 6:31 am
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.