문제

I'm using PDO to generate the following query:

  INSERT INTO MyTable (ID, Data) 
  VALUES ( 
    (:id_0, :data_0), (:id_1, :data_1), (:id_2, :data_2), 
    (:id_3, :data_3), (:id_4, :data_4), (:id_5, :data_5), 
    (:id_6, :data_6), (:id_7, :data_7), (:id_8, :data_8), 
    (:id_9, :data_9)  
  )

this query is generated inside a loop so i just got the print_r($query); part and pasted here.

Then in my PHP i have a loop that binds the parameters like this:

   $c = 0;
   foreach($data as $key=> $value)
   {
        $insert->bindValue(":id_{$c}", $key, DB::PARAM_INT);
        $insert->bindValue(":data_{$c}", $value, DB::PARAM_STR);
        $c++;
   }

i get the following error:

SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 1 column(s)

My Table Definition looks like this:

   CREATE TABLE MyTable(
        ID INT PRIMARY KEY,
        Data TEXT
    ) ENGINE=MyISAM

can anyone help me?

 $data - this just holds key - value pairs where key is integer and value
 is serialized array.
도움이 되었습니까?

해결책

Remove the outer () from your VALUES clause. A multi-row VALUES clause is not () enclosed, but each comma-separated row group is () enclosed as in VALUES (1,2,3),(3,2,1),(1,2,3). By enclosing the whole row list, MySQL must be misinterpreting the outer () as the start of an expression whose result would be the first column.

VALUES /* no ( here... */
    (:id_0, :data_0), (:id_1, :data_1), (:id_2, :data_2), 
    (:id_3, :data_3), (:id_4, :data_4), (:id_5, :data_5), 
    (:id_6, :data_6), (:id_7, :data_7), (:id_8, :data_8), 
    (:id_9, :data_9)  
/* no ) here... */
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top