PDO准备在单个查询中插入多行我目前正在MySQL上使用这种类型的SQL在一个查询中插入多行值:INSERT INTO `tbl` (`key1`,`key2`) VALUES ('r1v1','r1v2'),('r2v1','r2v2'),...在PDO的阅读中,使用准备好的语句应该比静态查询提供更好的安全性。因此,我想知道是否可以使用预先准备的语句生成“使用一个查询插入多行值”。如果是,我可以知道如何实施吗?
3 回答
海绵宝宝撒
TA贡献1809条经验 获得超8个赞
<?phprequire('conn.php');$fname = 'J';$lname = 'M';$time_start = microtime(true);$stmt = $db-> prepare('INSERT INTO table (FirstName, LastName) VALUES (:fname, :lname)');for($i = 1; $i <= 10; $i++ ) { $stmt->bindParam(':fname', $fname); $stmt->bindParam(':lname', $lname); $stmt->execute(); $fname .= 'O'; $lname .= 'A';}$time_end = microtime(true);$time = $time_end - $time_start;echo "Completed in ". $time ." seconds <hr>"; $fname2 = 'J';$lname2 = 'M';$time_start2 = microtime(true);$qry = 'INSERT INTO table (FirstName, LastName) VALUES '; $qry .= "(?,?), ";$qry .= "(?,?), ";$qry .= "(?,?), ";$qry .= "(?,?), ";$qry .= "(?,?), ";$qry .= "(?,?), ";$qry .= "(?,?), "; $qry .= "(?,?), ";$qry .= "(?,?), ";$qry .= "(?,?)";$stmt2 = $db->prepare($qry);$values = array();for($j = 1; $j<=10; $j++) { $values2 = array($fname2, $lname2); $values = array_merge($values,$values2); $fname2 .= 'O'; $lname2 .= 'A';}$stmt2->execute($values);$time_end2 = microtime(true);$time2 = $time_end2 - $time_start2;echo "Completed in ". $time2 ." seconds <hr>";?>
- 3 回答
- 0 关注
- 522 浏览
添加回答
举报
0/150
提交
取消