note 92887 added to function.mysql-real-escape-string

From: Date: Thu, 13 Aug 2009 05:09:53 +0000
Subject: note 92887 added to function.mysql-real-escape-string
Groups: php.notes 
Request: Send a blank email to php-notes+get-159430@lists.php.net to get a copy of this message
@Anonymous from 8/2/2009 - Your code will only work so long as $_POST is not a multidimensional array. Example: <?php $array = array('onedeep', array(array('threedeep'),'twodeep')); foreach($array as $key => $value) { $array = mysql_real_escape_string($value); } ?> This spits out a warning: "mysql_real_escape_string() expects parameter 1 to be string, array given". There are plenty of real-world cases where $_POST will be a multidimensional array. You can get around this by writing a recursive escape function, such as this: <?php function mysql_escape_array($input){ if(is_array($input)){ foreach($input as $key=>$value){ $output[$key]=mysql_escape_array($value); } }else{ $output=mysql_real_escape_string($input); } return $output; } ?> There are ways to improve upon this (checking for magic quotes status, etc). ---- Server IP: 92.48.74.199 Probable Submitter: 32.135.31.152 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-real-escape-string.php Edit -- https://master.php.net/note/edit/92887 Del: integrated -- https://master.php.net/note/delete/92887/integrated Del: useless -- https://master.php.net/note/delete/92887/useless Del: bad code -- https://master.php.net/note/delete/92887/bad+code Del: spam -- https://master.php.net/note/delete/92887/spam Del: non-english -- https://master.php.net/note/delete/92887/non-english Del: in docs -- https://master.php.net/note/delete/92887/in+docs Del: other reasons-- https://master.php.net/note/delete/92887 Reject -- https://master.php.net/note/reject/92887 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#159430) next »