简体   繁体   中英

PDO fails to prepare a statement with over 13 placeholders

this is the code I'm using:

self::$DB->prepare($query, $types);

when the $query and types are:

//$query
UPDATE Permisos
    SET                 
        empleado_id = ?,
        agregar_mensaje = ?,
        borrar_mensaje = ?,
        agregar_noticia = ?,
        borrar_noticia = ?,
        agregar_documento = ?,
        borrar_documento = ?,
        agregar_usuario = ?,
        borrar_usuario = ?,
        agregar_empresa = ?,
        borrar_empresa = ?,
        agregar_tarea = ?
    WHERE
        id = ?
//$types
Array(
    [0] => integer
    [1] => boolean
    [2] => boolean
    [3] => boolean
    [4] => boolean
    [5] => boolean
    [6] => boolean
    [7] => boolean
    [8] => boolean
    [9] => boolean
    [10] => boolean
    [11] => boolean
    [12] => integer
)

Everything works great, but when they are:

//$query
UPDATE Permisos SET                         
         empleado_id = ?,
         agregar_mensaje = ?,
         borrar_mensaje = ?,
         agregar_noticia = ?,
         borrar_noticia = ?,
         agregar_documento = ?,
         borrar_documento = ?,
         agregar_usuario = ?,
         borrar_usuario = ?,
         agregar_empresa = ?,
         borrar_empresa = ?,
         agregar_tarea = ?,
         borrar_tarea = ?
    WHERE
id = ?
//$types
Array(
        [0] => integer
        [1] => boolean
        [2] => boolean
        [3] => boolean
        [4] => boolean
        [5] => boolean
        [6] => boolean
        [7] => boolean
        [8] => boolean
        [9] => boolean
        [10] => boolean
        [11] => boolean
        [12] => boolean
        [13] => integer
    )

It fails with the following message:

<b>Warning</b>:  PDO::prepare() [<a href='pdo.prepare'>pdo.prepare</a>]: SQLSTATE[HY000]: General error: PDO::ATTR_STATEMENT_CLASS requires format array(classname, array(ctor_args)); the classname must be a string specifying an existing class in <b>C:\wamp\www\intratin\JP\includes\empleado\mapper\Permiso.php</b> on line <b>137</b><br />


Doesn't matter which field I add or remove, it fails every time with more than 13 placeholders.

If you self::$DB->prepare method is actually calling PDO::prepare , make sure that you don't pass that $types argument as a second parameter to PDO::prepare

Judging from the documentation, the second parameter that PDO::prepare expects is an array of options -- not an array describing the type of data for each placeholder.


And you try to execute this portion of code :

var_dump(PDO::ATTR_STATEMENT_CLASS);

You'll get this output :

int 13


Which kind of explains the error :

  • You are trying to pass to PDO::prepare and array as second parameter
  • PDO::prepare expects that array to contain a list of options
  • You have an element with key 13 in your array
  • 13 is PDO::ATTR_STATEMENT_CLASS
  • PDO::preapre expects something specific for the PDO::ATTR_STATEMENT_CLASS option
    • like something corresponding to array(classname, array(ctor_args)); , judging from your error message
  • You're passing integer instead of that
  • So you get an error.


Not sure how you can specify the types of each bound parameters with the class you are using -- but it seems it's not as a second parameter to prepare ;-)

And, if your self::$DB is indeed an instance of PDO, I don't find a method that would allow you to specify the types of all parameters at once -- it seems you have to specify the type for each parameter, each time you call either bindParam or bindValue .

You can do the following, if you don't care too much about the types (AFAIK they'll all be PDO::PARAM_STR; you can't specify them when you're not using bindValue/bindParam):

$params = array('your', 'params', 'here', '...'); // just the params, not the type
$stmnt = self::$DB->prepare($query);
$stmnt->execute($params);

See PDOStatement's execute method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM