How do I add more than one row with Zend_Db?

前端 未结 6 875
暗喜
暗喜 2020-12-08 07:57

I have an array with information which looks more or less like this:

$data[] = array(\'content\'=>\'asd\');
$data[] = array(\'content\'=>\'asdf\');
         


        
6条回答
  •  一向
    一向 (楼主)
    2020-12-08 08:37

    to insert multiple rows you can use Zend_Db

    $stmt = $table->getAdapter()->prepare('INSERT INTO tablename (col1, col2, col3) VALUES (?, ?, ?), (?, ?, ?)');
    
    $stmt->execute( array($value1, $value2, $value3, $value4, $value5, $value6) );
    

    (from Bill Karwin)

    in your case we can change that to this code:

    $data[] = array('content'=>'asd');
    $data[] = array('content'=>'asdf');
    
    $stmt = $table->getAdapter()->prepare('INSERT INTO table (col1) VALUES (?), (?)');
    $stmt->execute( $data );
    

    to generate these '(?), (?)' dynamically, incase the data is dynamic, you can try using this snippet:

    $values = implode(',',array_fill(0,count($data),'(?)'));
    

    hope this helps

    regards, Riki Risnandar

提交回复
热议问题