跃然一笑
准备SQL语句一次,然后执行几次:<?php $Mysqli = new mysqli("host","user","pass","base");// check connectionif(mysqli_connect_errno()){ printf("Connect failed: %s\n",mysqli_connect_error()); exit();}// some data for db insertion$countries=['Austria','Belgia','Croatia','Denmark','Estonia'];// explicitly begin DB transaction$Mysqli->begin_transaction();// prepare statement (for multiple inserts) only once$stmt=$Mysqli->prepare("INSERT INTO table(column) VALUES(?)");// bind (by reference) prepared statement with variable $country$stmt->bind_param('s',$country);// load value from array into referenced variable $countryforeach($countries as $country){ //execute prep stat more times with new values //$country is binded (referenced) by statement //each execute will get new $country value if(!$stmt->execute()) { // rollback if prep stat execution fails $Mysqli->rollback(); // exit or throw an exception exit(); }}// close prepared statement$stmt->close();// commit transaction$Mysqli->commit();// close connection$Mysqli->close();?>