24

I am aware of array_walk() and array_map(). However when using the former like so (on an old project) it failed

array_walk($_POST, 'mysql_real_escape_string');

Warning: mysql_real_escape_string() expects parameter 2 to be resource, string given.

So I went with this slightly more ugly version

foreach($_POST as $key => $value) {
    $_POST[$key] = mysql_real_escape_string($value);
}

So why didn't the first way work? What is the best way to map values of an array to a function?

hakre
  • 193,403
  • 52
  • 435
  • 836
alex
  • 479,566
  • 201
  • 878
  • 984

7 Answers7

41

The callback function passed to array_walk is expected to accept two parameters, one for the value and one for the key:

Typically, funcname takes on two parameters. The array parameter's value being the first, and the key/index second.

But mysql_real_escape_string expects the second parameter to be a resource. That’s why you’re getting that error.

Use array_map instead, it only takes the value of each item and passes it to the given callback function:

array_map('mysql_real_escape_string', $_POST);

The second parameter will be omitted and so the last opened connection is used.

If you need to pass the second parameter, you need to wrap the function call in another function, e.g. an anonymous function:

array_map(function($string) use ($link) { return mysql_real_escape_string($string, $link); }, $_POST);
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • 4
    `array_map()` is also a better choice because it does not modify the original array, it returns a new array after the function is applied. – kapa Feb 01 '13 at 10:36
4

I know the OP asked to call a function, however in the cases where you do not really need to call a function you can define an anonymous one:

$ids = [1,2,3];
array_walk($ids,function(&$id){$id += 1000;});
rynop
  • 50,086
  • 26
  • 101
  • 112
3

It's because the mysql_real_escape_string is being given two parameters, both string.

http://php.net/manual/en/function.mysql-real-escape-string.php

http://www.phpbuilder.com/manual/en/function.array-map.php:

array_map() returns an array containing all the elements of arr1 after applying the callback function to each one. The number of parameters that the callback function accepts should match the number of arrays passed to the array_map()

you could do

function myescape($val)
{
    return mysql_real_escape_string($val);
}

... then

array_walk($_POST, 'myescape');
alex
  • 479,566
  • 201
  • 878
  • 984
John Boker
  • 82,559
  • 17
  • 97
  • 130
3

http://php.net/manual/en/function.array-walk.php says that array_walk will call the function with 2 arguments, the value and the key. You should write a new function to wrap mysql_real_escape_string. Something like:

function wrapper($val, $key){
    return mysql_real_escape_string($val);
}

And then:

array_walk($_POST, 'wrapper');

Sorry if my PHP is not correct, but I think you'll catch the general idea.

kapa
  • 77,694
  • 21
  • 158
  • 175
Juanjo Conti
  • 28,823
  • 42
  • 111
  • 133
3

mysql_real_escape_string() won't work unless you've made a mysql connection first. The reason it is so cool is that it will escape characters in a way to fit the table type. The second [optional] argument is a reference to the mysql connection.

$_POST is always set up as key->value. So, you array_walk calls mysql_real_escape_string(value, key). Notice the second argument is not a reference.

That is why it does not work. There are several solution already mentioned above.

alex
  • 479,566
  • 201
  • 878
  • 984
the Hampster
  • 876
  • 1
  • 14
  • 21
0

PHP 7.4 style:

$link = mysqli_connect("localhost", "my_user", "my_password", "world");

array_map(fn($string) => mysqli_real_escape_string($string, $link), $_POST);
Dmitry Leiko
  • 3,970
  • 3
  • 25
  • 42
-2

I had trouble using the accepted answer as it caused me errors for reasons I couldn't work out. So for anyone having trouble with array_walk or array_map I found this to work.

foreach($_POST as $pk => $pv) $_POST[$pk] = mysql_real_escape_string($pv);
Gga
  • 4,311
  • 14
  • 39
  • 74
  • Have you asked a question about what was the problem? This way this answer is simply spreading a myth that there is some kind of problem with `array_map()`. -1 – kapa Feb 01 '13 at 09:19
  • @bažmegakapa This isn't spreading any myth. This is just providing an alternative, simpler to understand, solution for those less familiar with php's functions. The OP asked for an easy way to apply a function to an array, which this does... What indeed are you talking about anyway? – Gga Feb 01 '13 at 09:30
  • I did not even mention that the same code you posted as an answer is already in the question... – kapa Feb 01 '13 at 10:38
  • @bažmegakapa Good point, not sure how that happened. Still though, your myth theory is a bit stupid. – Gga Feb 01 '13 at 10:43
  • 2
    So in just a few words: there is no problem with `array_map()` and `array_walk()`. You are talking about some strange errors appearing, 99% because you did something wrong. You could not fix it, and you are advising other people to use `foreach` because of that, instead of fixing their code or spending a little time to understand optimized array functions. I have no problem with `foreach`, just the reasoning. No insult intended. – kapa Feb 01 '13 at 10:59
  • 1
    @bažmegakapa fair enough, and yes was just stating I couldn't figure it out but this was an approach that worked. Wasn't advocating it over any other approach or citing any issue with array_map() and array_walk(), just stating this worked for me really. – Gga Feb 01 '13 at 11:08