Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with array_walk_recursive and mysqli::real_escape_string

Tags:

php

mysql

I use object oriented style for my mysql connection, but if I write this:

array_walk_recursive($_POST, array($mysqli, 'real_escape_string'));

I get this error:

Warning: mysqli::real_escape_string() expects exactly 1 parameter, 2 given


The fact is that mysqli::escape_string accept only 1 parameter:

string mysqli::escape_string ( string $escapestr )

I write:

$VAR = array();
$VAR = $_POST;

function escape_string($item, $key) {
    $arr[$key] = $mysqli->real_escape_string($item);
}

array_walk_recursive($VAR, 'escape_string');

And I get this error:

Fatal error: Call to a member function real_escape_string() on a non-object
like image 498
Drew Avatar asked Aug 15 '26 14:08

Drew


1 Answers

You need to pass a string as the second argument, not an array:

function escape_string($item, $key) {
    // Echo them out (using procedural mysqli)
    echo mysqli_real_escape_string($item);
    // or collect them in an array (using OOP mysqli)
    $arr[$key] = $mysqli->real_escape_string($item);
}

array_walk_recursive($_POST, 'escape_string');
like image 70
hohner Avatar answered Aug 17 '26 03:08

hohner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!