Penggunaan asas pernyataan yang disediakan
1, Penggunaan penyata SQL yang disediakan yang disediakan
<?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(?,?)"); }catch (PDOException $exception){ echo $exception->getMessage().'<br>'; }e
2, parameter mengikat
<?php $name='java基础教程'; $author='smile4'; $stmt->bindParam(1,$name); $stmt->bindParam(2,$author);e
3, melaksanakan penyataan yang disediakan
<?php $stmt->execute();e
all kod adalah seperti berikut:
<?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(?,?)"); $name='java基础教程'; $author='smile4'; $stmt->bindParam(1,$name); $stmt->bindParam(2,$author); $stmt->execute(); //$sql='select *from book'; //$result=$pdo->query($sql); //$row=$result->fetchAll(); //echo "<pre>"; //print_r($row); //echo "</pre>"; }catch (PDOException $exception){ echo $exception->getMessage().'<br>'; }e
Paparan hasil pelaksanaan penyemak imbas:
Berfikir: Bagaimana untuk menambah data dalam kelompok dan mencegah suntikan SQL? (Akan diperkenalkan dalam dua bahagian seterusnya)