如何在zend框架中构建多个插入查询[重复]

时间:2022-09-24 00:17:37

Possible Duplicate:
How do I add more than one row with Zend_Db?

可能重复:如何使用Zend_Db添加多行?

i would like to build this query

我想构建此查询

INSERT INTO ad-page (ad_name, page_name) VALUES ('value1', 'value2'), ('value3', 'value4') , ....

i tried this which did not work

我试过这个没用的

        $adpagemodel = new Admin_Model_AdPage();

        if(count($adpage)> 0)
            foreach($adpage as $page)
            {
                $newdatap[]['page_name'] = $page;
                $newdata[]['ad_name'] = $adname;            
            }
        $adpagemodel->insert($newdata); 

and please also check this

还请检查一下

2 个解决方案

#1


21  

There is simple option. Create the query by hand ;)

有简单的选择。手动创建查询;)

like this:

$query = 'INSERT INTO ' . $db->quoteIdentifier('table') . ' (`col1`, `col2`) VALUES ';
$queryVals = array();
foreach ($data as $row) {
    foreach($row as &$col) {
        $col = $db->quote($col);
    }
    $queryVals[] = '(' . implode(',', $row) . ')';
}
$stmt = $db->query($query . implode(',', $queryVals));

#2


2  

Not all databases support this. So, there is no universal answer. But if you would tell what's you DB, we might suggest some trick... :-)

并非所有数据库都支持此功所以,没有普遍的答案。但如果你告诉你什么是DB,我们可能会建议一些技巧...... :-)

#1


21  

There is simple option. Create the query by hand ;)

有简单的选择。手动创建查询;)

like this:

$query = 'INSERT INTO ' . $db->quoteIdentifier('table') . ' (`col1`, `col2`) VALUES ';
$queryVals = array();
foreach ($data as $row) {
    foreach($row as &$col) {
        $col = $db->quote($col);
    }
    $queryVals[] = '(' . implode(',', $row) . ')';
}
$stmt = $db->query($query . implode(',', $queryVals));

#2


2  

Not all databases support this. So, there is no universal answer. But if you would tell what's you DB, we might suggest some trick... :-)

并非所有数据库都支持此功所以,没有普遍的答案。但如果你告诉你什么是DB,我们可能会建议一些技巧...... :-)