Skip to content
Advertisement

Error with PHP parameters in SQL function call

I have two PHP variables in a class that are integers ($id and $descCode).

I’m trying to get these into my SQL function call as characters (the database is looking for these to be CHAR 2 and CHAR 10 respectively).

For some reason, this is triggering an error:

Use of parameter marker or NULL not valid

What exactly am I doing wrong here?

$results = array();
$results = $db->select("SELECT newCodeTest(:id,:desc) as newCode FROM testTable",
    [
        'id' => (string)$id,
        'desc' => (string)$descCode
    ]
);

Advertisement

Answer

You can not use PDO in this way.

Look at this (possible duplicate): Can PHP PDO Statements accept the table or column name as parameter?.

Set those values as basic string

$id = $pdo->quote($id);
$desc = $pdo->quote($desc);
"SELECT newCodeTest({$id},{$desc}) as newCode FROM testTable"

Info about quoting:

https://www.php.net/manual/en/pdo.quote.php

Interesting info about performance

https://www.php.net/manual/en/pdo.quote.php#122967

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement