Home > Article > Backend Development > php+mysqli implements batch execution of insert, update and delete data methods, phpmysqli_PHP tutorial
The example in this article describes the method of inserting, updating and deleting data in batches using php+mysqli. Share it with everyone for your reference. The details are as follows:
mysqli executes insert/update/delete data in batches, the function is multi_query().
The following code only inserts data in batches. In fact, inserting data, updating data, deleting data, etc. can be mixed and executed together. This function is used when querying. Reading data is a little different. Please refer to the following code for details:
<?php //1、创建数据库连接对象 $mysqli = new MySQLi("localhost","root","123456","liuyan"); if($mysqli->connect_error){ die($mysqli->connect_error); } $mysqli->query("set names 'GBK'"); //2、数据插入语句 $sqls = "insert into news(title,contents,datetime,hit) values('去哪儿网架构调整铺垫上市','据接近去哪儿网的消息人士透露,事业部以运营、收入、利润进行考核;新业务部则是以产品运营、商业模式、总亏损进行考核。','".date('Y-m-d H:i:s')."',30);"; $sqls .= "insert into news(title,contents,datetime,hit) values('告诉金主 视频网站谁是NO.1','搜狐说,自己稳稳超过土豆,是行业老二;腾讯说,自己已经超过了优酷,成为行业第一;土豆不承认腾讯的计算方法,坚称自己仍然是亚军;爱奇艺则摆出数据称在单用户观看时长方面排名第一位。','".date('Y-m-d H:i:s')."',30);"; $sqls .= "insert into news(title,contents,datetime,hit) values('苹果正滋生傲慢情绪','据国外媒体报道,没有经历重大挫折的成功往往会让人滋生傲慢。在过去10年中,苹果取得了巨大的成功,成为了音乐播放器和手机市场上的领导者,但却没未遭遇任何较大的挫折。','".date('Y-m-d H:i:s')."',30);"; //3、执行,注意批量执行函数 $res = $mysqli->multi_query($sqls);//返回的是布尔值 //4、判断是否执行成功 if(!$res){ echo "数据插入失败"; }else{ echo "成功!!!"; } //5、关闭连接 $mysqli->close(); ?>
I hope this article will be helpful to everyone’s PHP programming design.