escape array?

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
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

escape array?

Post 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.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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);
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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)
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

Awesome, thanks a lot!
Post Reply