I'm having a problem outputing from a database


I'm trying to get a text field from the MySQL data base

My input was "Come join us for John's birthday"

But the actual out put was "Come join us for John\'s birthday"

How do I fix this?

When i do " " it comes out as \" \" how do I get rid of these literal slashes.

$result_resource = mysql_query("SELECT field FROM table WHERE condition", $connection_resource);
$result_value = mysql_result($result_resource, "field", 0);
echo stripslashes($result_value);

Something like that, though hopefully your variables are named a bit better than mine. ^.=.^ The important part is the stripslashes() function. This is what will clean those pesky extra slashes from your submitted code.

- Sen

What exactly causes these slashes to be printed out when the original data doesn't have them?

Heh, PHP puts them in to protect your information from being misinterpreted. It's called escaping. Basically, strings are usually represented in your code with 'string' or "string". But what if you want to put special characters in your string? Say you want to put a ' in your 'string'? You have to have some way to tell PHP that you are putting the ' inside the string, not ending it. Thus, PHP uses the C/C++ convention of escaping certain characters. So saying string's would become 'string\'s'. With strings in single quotes (') there are fewer characters that have to be escaped - but in turn there are fewer escapes that you can actually use. Double quotes (") are more flexible, but in turn they're more restrictive.

Single Quotes:                  Double Quotes:                        

--------------------------Must Escape---------------------------
\'                              \'
\\ (only if before '            \\
        or at end of string)    \"
                                 \$

---------------------------Can Escape---------------------------
\'                              \'
\\ (only if before '            \\
        or at end of string)    \"
                                \n      This is the newline.  It
                                                does the same
                                                thing the Enter
                                                key does in your
                                                favorite text
                                                editor.
                                \r      This is the linefeed.
                                                You probably
                                                won't ever use
                                                it.
                                \t      This is a tab.  Does the
                                                same thing as
                                                Tab on your
                                                keyboard.
                                \$      Displays a $ character.
                                                It needs to be
                                                escaped because
                                                PHP translates
                                                $variable inside
                                                double quotes
                                                into $variable's
                                                contents.  This
                                                tells PHP to use
                                                the $ character
                                                instead of
                                                trying to expand
                                                $variable.
                                \x##    The ## is a Hexadecimal 
                                                version of the 
                                                ASCII code you
                                                want to use.
                                                Probably won't
                                                use it much.
                                \###    The ### here is an octal
                                                version of the
                                                ASCII code you
                                                want to use.
                                                Probably won't
                                                use this one
                                                much, either.

As you can guess, single quotes are prefered for normal strings, but sometimes double quotes are required to get a job done. PHP assumes that you've passed it a double-quoted string, and escapes everything accordingly. The stripslashes function allows you to output the escape-free string, which is just how the user entered it.

Hopefully this helps! For more information, see the PHP manual: http://www.php.net/manual/en/

Good luck, and enjoy PHP!

- Sendoshin

So, now that we've got the "John\'s" things fixed, I had another question, how do i make the " " show? Like say my input was "Pho"netic, how do i Make it show? The output is currently nothing.


Actually I was looking at the database, it doesn't seem like the output was the one causing problems. I think its the input.

say i put the word Chillin' into the database from a form, it stores into the database as Chillin\' so when I recall the data, it comes out as such. And when I put " " around anyhting it stores into the database as \ and nothing else.

How do I make the input store correctly?

mysql_query("INSERT INTO events (creator, event, location, date, time, description) VALUES ('$creator', '$event', '$location', '$date', '$time', '$description')");

HMm, The problem is definately before the application even proceses into the database. When it turns my string into a variable, it adds the \'s for some reason.

How do you fix it?

This article has been dead for over six months. Start a new discussion instead.