Preprocessing batch added data
In the previous section, a piece of data was added. Batch addition only requires a foreach loop to traverse and add.
The code is as follows:
<?php //绑定参数 $stmt->bindParam(1,$name); $stmt->bindParam(2,$author); //单条插入 //$name='java基础教程'; //$author='smile4'; //$stmt->execute(); //批量插入 $data=array( array('php预处理批量添加教程1','smile'), array('php预处理批量添加教程2','smile'), array('php预处理批量添加教程3','smile'), ); foreach ($data as $row){ $name=$row[0]; $author=$row[1]; $stmt->execute(); }
The complete code is as follows:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/5 0005 * Time: 上午 9:23 */ header("Content-Type:text/html;charset=utf-8"); //mysql:host:localhost;port=3306;dbname=php;charset=utf-8 $dbms='mysql'; $host='localhost'; $port='3306'; $dbname='php'; $charset='utf-8'; //用户名与密码 $user='root'; $pwd='root'; $dsn="$dbms:host=$host;port=$port;dbname=$dbname;charset=$charset"; try{ $pdo=new PDO($dsn,$user,$pwd); //预处理sql语句 $stmt=$pdo->prepare("insert into book(name,author)values(?,?)"); //绑定参数 $stmt->bindParam(1,$name); $stmt->bindParam(2,$author); //单条插入 //$name='java基础教程'; //$author='smile4'; //$stmt->execute(); //批量插入 $data=array( array('php预处理批量添加教程1','smile'), array('php预处理批量添加教程2','smile'), array('php预处理批量添加教程3','smile'), ); foreach ($data as $row){ $name=$row[0]; $author=$row[1]; $stmt->execute(); } //$sql='select *from book'; //$result=$pdo->query($sql); //$row=$result->fetchAll(PDO::FETCH_ASSOC); //echo "<pre>"; //print_r($row); //echo "</pre>"; }catch (PDOException $exception){ echo $exception->getMessage().'<br>'; }
Execution result display:
#You can also add parameters to the execute() function. The parameter type is an array. The number of elements in the array must be the same as the number of placeholders
All code display:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/5 0005 * Time: 上午 9:23 */ header("Content-Type:text/html;charset=utf-8"); //mysql:host:localhost;port=3306;dbname=php;charset=utf-8 $dbms='mysql'; $host='localhost'; $port='3306'; $dbname='php'; $charset='utf-8'; //用户名与密码 $user='root'; $pwd='root'; $dsn="$dbms:host=$host;port=$port;dbname=$dbname;charset=$charset"; try{ $pdo=new PDO($dsn,$user,$pwd); //预处理sql语句 $stmt=$pdo->prepare("insert into book(name,author)values(?,?)"); //绑定参数 //$stmt->bindParam(1,$name); //$stmt->bindParam(2,$author); //单条插入 //$name='java基础教程'; //$author='smile4'; //$stmt->execute(); //批量插入 $data=array( array('php预处理批量添加教程1','smile'), array('php预处理批量添加教程2','smile'), array('php预处理批量添加教程3','smile'), ); foreach ($data as $row){ // $name=$row[0]; // $author=$row[1]; $stmt->execute($row); } //$sql='select *from book'; //$result=$pdo->query($sql); //$row=$result->fetchAll(PDO::FETCH_ASSOC); //echo "<pre>"; //print_r($row); //echo "</pre>" }catch (PDOException $exception){ echo $exception->getMessage().'<br>'; } ;
Note:Analysis mentioned above The execute() code you get can know the array index used when passing multiple pieces of data using the "?" placeholder,
, which is
array(
array('php preprocessing batch addition tutorial 1','smile'),
array('php preprocessing batch addition tutorial 2','smile'),
array('php preprocessing batch addition Tutorial 3','smile'),
);type array
If you use the placeholder of (: parameter name), you need to use an associative array when inserting multiple items, that is, an array Index is specific typed data
For example:
array(
array("name"=>'php preprocessing batch addition tutorial 1',"author "=>'smile'),
## array("name"=>'php preprocessing batch addition tutorial 1',"author"=>'smile'),
## array("name"=>'php preprocessing batch addition tutorial 1',"author"=>'smile'));