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
Problems with Ñ in PHP
Moderator: General Moderators
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Problems with Ñ in PHP
Try changing the charset to iso-8859-1 or have a look at this url
http://htmlhelp.com/reference/html40/en ... atin1.html
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
Re: Problems with Ñ in PHP
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
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,'Ñ',$LastName);
the value of $LastName = DE LA PEÑA
here is my code:
$inye = array("Ñ","ñ");
$lname = str_replace($inye,'Ñ',$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
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!
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
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 ?
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Problems with Ñ in PHP
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
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.