1

I have to work with the following PDO connection query:

$fields = array(
        'titulo',
        'tipo_produto',
        'quantidade_peso',
        'unidade_de_venda',
        'unidades_por_caixa',
        'caixas_piso',
        'pisos_palete',
        'tipo_de_palete',
        'unidades_palete',
        'caixas_palete',
        'uni_diametro',
        'uni_largura',
        'uni_profundidade',
        'uni_altura',
        'uni_peso_bruto_unidade',
        'caixa_largura',
        'caixa_profundidade',
        'caixa_altura',
        'altura_palete',
        'volume_unidade',
        'volume_caixa',
        'volume_palete',
        'peso_caixa',
        'peso_palete'
    );
    $sql = 'INSERT INTO ficha_item ( %s ) VALUES ( %s )';
    $fieldsClause = implode(', ', $fields);

    $valuesClause = implode(', ', array_map(function($value) {
                        return ':' . $value;
                    }, $fields));

    $sql = sprintf($sql, $fieldsClause, $valuesClause);

    $stmt = $db->prepare($sql);

    $stmt->execute(array_intersect_key($_POST, array_flip($fields)));

This was returning me the following error:

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

After some digging around, it seems that anonymous functions were only implemented in PHP 5.3, for me to get this working on 5.2, I should use create_function, I'm still pretty new to PHP, so I'm having issues figuring out the right syntax here, I also don't really understand the $value variable and how to implement it correctly on create_function.

Looking for some help here or even alternatives to create_function.

4
  • 1
    You should rather update your PHP version. create_function is almost as messy as eval since it involves putting code in a string. Yuck! Commented Dec 31, 2012 at 12:22
  • Free gift: create_function('$value', 'return ":".$value;') - you literally put the argument list as a string in the first argument, and the function body code in the second. The only thing to be aware of is the quote types you use and how they will come out of the other end - you should echo the string you pass and make sure it is valid PHP code you are not sure. Although it's also often better to create a named function and just pass the function name as a string, it's usually more readable. The best solution though, as mentioned above, is to update your PHP version. Commented Dec 31, 2012 at 12:23
  • create_function('$value','return ":".$value;'); Commented Dec 31, 2012 at 12:23
  • Or You could just define that function elsewhere in the code and refrence it by name Commented Dec 31, 2012 at 12:24

2 Answers 2

4
$valuesClause = implode(', ', array_map(function($value) {
                    return ':' . $value;
                }, $fields));

change to:

$func = create_function('$value', 'return ":" . $value;');
$valuesClause = implode(', ', array_map($func, $fields));
Sign up to request clarification or add additional context in comments.

Comments

3

Better create normal function

function prefixWithColon($value) {
    return ':' . $value;
}

and use it's name as callback

$valuesClause = implode(', ', array_map('prefixWithColon', $fields));

Comments

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.