Apostrophe problem in results

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
trassalg
Forum Newbie
Posts: 23
Joined: Fri Jul 27, 2007 3:26 pm

Apostrophe problem in results

Post by trassalg »

When I sent articles to my database they enter find with apostrophes, but when I retrieve them from the database and have them sent to the browser to edit the text fields, they always return as the following:

\'

It seems MySQL is adding the slash so it doesn't confuse the apostrophe as the end or start of a command, but how do I go about stripping that slash when retrieving the results to my browser?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

what does

Code: Select all

foreach ( array('magic_quotes_gpc', 'magic_quotes_runtime', 'magic_quotes_sybase') as $p ) {
  echo $p, ': ', ini_get($p) ? 'On':'Off', "<br />\n";
}
print?
trassalg
Forum Newbie
Posts: 23
Joined: Fri Jul 27, 2007 3:26 pm

Post by trassalg »

magic_quotes_gpc: On
magic_quotes_runtime: Off
magic_quotes_sybase: Off
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

http://de2.php.net/manual/en/ref.info.php#ini.magic-quotes-gpc wrote: magic_quotes_gpc boolean

Sets the magic_quotes state for GPC (Get/Post/Cookie) operations. When magic_quotes are on, all ' (single-quote), " (double quote), \ (backslash) and NUL's are escaped with a backslash automatically.
Can you change the server configuration and turn this nonsense off?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You can disable them through .htaccess:

Code: Select all

# As my lovely host that thinks magic_quotes is a good thing informed me...
php_flag magic_quotes_gpc off
Or use this code, provided to me by members of the DevNet, at the start of all of your files (in the form of a config.php of some sort):

Code: Select all

// Strip slashes from all GPC data
if(get_magic_quotes_gpc())
{
    function strip_gpc_slashes(&$array)
    {
        if(!is_array($array))
        {
            return;
        }
        foreach($array as $key => $val)
        {
            is_array($array[$key]) ? strip_gpc_slashes($array[$key]) : ($array[$key] = stripslashes($val));
        }
    }
    
    $gpc = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST, &$_FILES);
    strip_gpc_slashes($gpc);
}
It's better to support and deal with magic_quotes than to just turn them off (until PHP 6 ^_^).
trassalg
Forum Newbie
Posts: 23
Joined: Fri Jul 27, 2007 3:26 pm

Post by trassalg »

volka wrote:
http://de2.php.net/manual/en/ref.info.php#ini.magic-quotes-gpc wrote: magic_quotes_gpc boolean

Sets the magic_quotes state for GPC (Get/Post/Cookie) operations. When magic_quotes are on, all ' (single-quote), " (double quote), \ (backslash) and NUL's are escaped with a backslash automatically.
Can you change the server configuration and turn this nonsense off?
I can cause at this point I'm just testing it locally, but once I upload the site to a server I'm not sure. Haven't tried yet.
Post Reply