header("Content-type: text/html; charset=gb2312");
$pdo=new pdo("mysql:host=localhost;dbname=news;charset=gbk","root","");
$datas=array(//初始化数据
array("hjf990911","123345","391"),
);
//批量添加信息
$sql="inserts into news(title,hit,content) values(?,?,?)";
$stmt=$pdo->prepare($sql);
foreach($datas as $data){
$stmt->execute($data);
}
//批量修改信息,?参数
$sql="updates news set title=?,hit=? where id=?";
$stmt=$pdo->prepare($sql);
foreach($datas as $data){
$stmt->execute($data);
}
//批量修改信息,占位参数
$sql="updates news set title=:title,hit=:hit where id=:id";
$stmt=$pdo->prepare($sql);
$datas=array(//初始化数据
array("title"=>"pdf990911","hit"=>"123345","id"=>"395"),
);
foreach($datas as $data){
$stmt->execute($data);
}
//查看信息
$sql="selects id,title from news order by id desc";
$stmt=$pdo->prepare($sql);
$stmt->execute();
$row=$stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($row as $vs){
var_dump($vs);
}
?>
如果觉得《php pdo批量操作 PDO使用二维数组批量操作MYSQL数据库》对你有帮助,请点赞、收藏,并留下你的观点哦!