Page 1 of 1

escape array?

Posted: Wed Mar 07, 2007 9:00 pm
by GeXus
I'm building an array through a loop,just a simple array... name[1], name[2], etc. How do you escape the contents of each array? mysql_escape_string won't work.

Posted: Wed Mar 07, 2007 9:48 pm
by dude81

Code: Select all

How do you escape the contents of each array
I didn't get it, Pasting a part of code may be of help

Posted: Thu Mar 08, 2007 3:31 am
by onion2k
Easy to understand way:

Code: Select all

for ($x=0;$x<count($arrMyArray);$x++) {
  $arrMyArray[$x] = mysql_escape_string($arrMyArray[$x]);
}
Nice way (Good if you need to pass extra information to the callback function):

Code: Select all

array_walk($arrMyArray,"mysql_escape_string");
Alternative way (Good if you need to carry out the same thing on more than one array):

Code: Select all

array_map("mysql_escape_string",$arrMyArray);

Posted: Thu Mar 08, 2007 5:02 am
by Mordred
onion2k wrote: Alternative way (Good if you need to carry out the same thing on more than one array):

Code: Select all

array_map("mysql_escape_string",$arrMyArray);
Should be:

Code: Select all

$arrMyArray = array_map("mysql_real_escape_string",$arrMyArray);
Only array_walk() works in-place.

Edit: better use mysql_real_escape_string, just to be more ... errr. .. real ;) (It requires a connection to the DB to be established first, which should have been done anyway)

Posted: Thu Mar 08, 2007 9:29 am
by GeXus
Awesome, thanks a lot!