<?php
try{
$pdo=new PDO("mysql:host=localhost;dbname=bookstore","root","158369");
echo "数据库连接成功!<br>";
}
catch(PDOexception $e){
echo "数据库连接失败!".$e->getmessage()."<br>";
}
try{
//1.传统的方式:将SQL发给数据库系统并直接执行
//$pdo->exec("insert into salary(name,age,education,salary)values('liu',23,'high',2222)");
//2.使用预处理类中的方法,先将SQL语句发给数据库系统保存后编译,但不直接执行
$statme=$pdo->prepare("insert into salary(name,age,educatino,salary)values(?,?,?,?)");
//使用预处理类中的方法,将上面SQL语句中的‘?’绑定给一个变量
$statme->bindParam(1,$name);
$statme->bindParam(2,$age);
$statme->bindParam(3,$education);
$statme->bindParam(4,$salary);
//将上述绑定的变量赋值
$name='liuhui';
$age=29;
$education='junior';
$salary=9000;
//执行上面准备好的SQL语句
$statme->execute();
//重复赋值并执行,插入多条语句
$name='liutao';
$age=28;
$education='Junior';
$salary=3547;
$statme->execute();
echo "数据插入成功!<br>";
}
catch(PDOexception $e){
echo "数据插入失败!".$e->getmessage();
}
?>
原文链接:https://blog.csdn.net/living_ren/article/details/77648535
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
还没有人抢沙发呢~