jump to navigation

Inserting Special Characters to MySQL using PHP September 20, 2006

Posted by ordinarywebguy in PHP.
trackback

After reading about Character Encodings, I got a solution to one of my task:

function spEncode($string) {
return base64_encode($string);
}

function spDecode($string) {
$string = base64_decode($string);
}

/*** Inserting ***/
$MySQL = “INSERT INTO table (field) VALUES (‘”. spEncode($field) .”‘)”;
mysql_query($MySQL,$MyDB) or die(mysql_error());

/*** Retrieve **/
$MySQL = “SELECT field FROM table “;
$MyQuery = mysql_query($MySQL or die(mysql_error());
while($row = mysql_fetch_row($MyQuery)) {
$field = spDecode($row[0]);
}
print nl2br($field);

Problem solved! :)

Comments»

1. thedandare - August 9, 2008

Hey, almost two years later it worked for me too.. Seems that the ajax posts are always uft-8, and i’m using latin1, besides im having trouble with single quotes in textarea forms.. Econding seems to work fine, the fallback is it makes the sql table unreadable.. thanks

2. Inserting Special Characters to MySQL (The Real One) « Just An Ordinary Web Guy - October 17, 2008

[...] 17, 2008 Posted by ordinarywebguy in MySQL, Uncategorized. trackback Regardless to my post about inserting special characters in mysql using PHP, this time we are not going to use PHP. Here’s [...]

3. Aryashree Pritikrishna - August 25, 2009

Thanks a lot.
Arya