1

I am getting a error saying this code needs a parentheses but every time so far it hasn't came up as a problem before here is the full error message:

Parse error: syntax error, unexpected T_FUNCTION, expecting ')'

Here is my code:the first line is where is says the error is:

$escaped_values = join(',', array_map(function($v) {
return "'" . mysql_real_escape_string($v) . "'";
}, array(
$_POST['account_status'],
$activation_key,
$_POST['first_name'],
$_POST['last_name'],
$_POST['email'],
$_POST['username'],
$password,
$_POST['phone_number']
)));
2
  • 3
    What version of PHP are you using? Anonymous functions were introduced in 5.3.0, so if you have an older version of PHP, you'll run into this error. On another note, have you considered using PDO's parameterized queries? Easier, cleaner and universally understood. Commented Oct 18, 2012 at 20:19
  • @Alfabravo I am running a later version (5.2.17) Commented Oct 18, 2012 at 20:22

2 Answers 2

3

Sounds like you are using an older version of PHP that doesn't support closures. They are available as of PHP 5.3

Example using an old fashioned named function:

function escape($v)
{
    return "'" . mysql_real_escape_string($v) . "'"; 
}

$escaped_values = join(',', array_map('escape', array(
    $_POST['account_status'],
    $activation_key,
    $_POST['first_name'],
    $_POST['last_name'],
    $_POST['email'],
    $_POST['username'],
    $password,
    $_POST['phone_number']
)));

However you'd better do it using PDO as suggested by NullUserException...

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah I am just realized that I am Running 5.2.17
Use an old fashioned named function, and pass the name of that function as a string to the first parameter of array_map, see the examples in the manual: php.net/array_map
so how would that look as code?I looked at php.net but still don't really get it.I also need it to be a mySQL escaped string.
2

Anonymous functions were introduced in 5.3.0, so if you have an older version of PHP, you'll run into this error. Another way to do what you're doing is it to use create_function():

array_map(create_function('$v', 'return "\'" . mysql_real_escape_string($v) . "\'";')
           , $yourArray)

But a far better alternative would be to use PDO and parameterized queries:

// example:
$sth = $dbh->prepare('INSERT INTO your_table
                          (activation_status, first_name, last_name)
                             VALUES (?, ?, ?)');

$params = array($activation_status, $_POST['first_name'], $_POST['last_name']);
$sth->execute($params);

Easier, cleaner and universally understood. Your parameters will be automatically escaped.

1 Comment

PDO is probably a kajillion times easier than using mysql_real_escape_string everywhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.