Using stripslashes on an array (Echoing db fields)

[size=8pt] I am not a php coder, nor will I ever be. It is however too useful to ignore in certain situations. As a result I know just enough php to hack some scripts/functions/etc together into something functional. Occasionally I run up against a situation where several hours of research has failed to provide an answer I can fathom. Hence this question. Not lazy, just stumped. [/size]

Using php to upload form fields to mysql db (unusual application huh?). Works fine and takes usual security steps to prevent injection with or without magicquotes. As a result some of the user entered text strings contain \ characters within the db fields. Again, expected and not a problem. Problem lies when attempting to retrieve and display data by echoing results of a fetched array. Example code below (Assumes connection to db exists)

[php] $data = mysql_query(“SELECT * FROM table_name”) or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
Echo “” . $info[‘field_one’] . “
”;
Echo “” . $info[‘field_two’] . “
”;
Echo “” . $info[‘field_three’] . “
”;
Echo “” . $info[‘field_four’] . “
”;
}
[/php]
Understand from my reading, that I cannot use stripslashes function on the entire array ($info) but the explanations and syntax in the examples I found on how to explicitly strip slashes from individual fields (Don’t need to do all of them) were beyond my ability to absorb.

An example of how to explicitly strip slashes from say 'field_two' and 'field_four' while not bothering with the rest, inserted into the above snippet would be a tremendous help.

Again, not interested in altering the actual db entry, just want " O'Neill " not to echo as O\"Neill.

TIA

you can use stripslashes on an individual element within the loop like this:

[php]
$data = mysql_query(“SELECT * FROM table_name”) or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
echo “” . $info[‘field_one’] . “
”;
echo “” . stripslashes($info[‘field_two’]) . “
”;
echo “” . $info[‘field_three’] . “
”;
echo “” . $info[‘field_four’] . “
”;
}
[/php]

Thanks Dave

Brackets always get me… Plain Square Curly So many choices

Turns out the real problem lay in the upload script. In my infinite ignorance I assumed the escape characters showing up were not an aberration when in fact the script was double escaping quotes NULL etc.
Fixed that and the problem went away

Thanks for taking the time to give me a solution, I saved the logic and am sure it will come in handy another time

Regards,
David

ah good glad you got t sorted. :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service