首页 > 代码库 > pdo 增删改查

pdo 增删改查

<?php
try{
//字符串统一用双引号,字符用单引号!
$pdo=new PDO("mysql:host=localhost;dbname=bagua","root","ECS_dyj365");
var_dump($pdo);
echo "<br/>";


//插入单条数据
$sql_i1="insert into test(username,password,email) values(‘dayan1‘,‘dyj365‘,‘dayan1@foxmail.com‘)";
//插入多条数据
$sql_i2="insert into test(username,password,email) values(‘dayan1‘,‘dyj365‘,‘dayan1@foxmail.com‘),
(‘dayan2‘,‘dyj365‘,‘dayan2@foxmail.com‘),(‘dayan3‘,‘dyj365‘,‘dayan3@foxmail.com‘)";

//删除单条数据
$sql_d1="delete from test where id=1";
//删除多条非连续数据
$sql_d2="delete from test where id in(19,20)";
//删除多条连续数据
$sql_d3="delete from test where id>=22 and id<=26";

//更新单条数据
$sql_u1="update test set username=‘lvfu‘ where id=1";
//更新多条非连续数据
$sql_u2="update test set username=‘lvfu‘ where id in(21,27)";
//更新多条连续数据
$sql_u3="update test set username=‘lvfu‘ where id>=28 and id<=29";

//查询所有数据
$sql_s1="SELECT * FROM test";
//查询单条数据
$sql_s2="select * from test where id=21";


$res=$pdo->exec($sql);
var_dump($res);
// echo "受影响的记录的条数为:".$res."<br/>";
// echo "最后插入的ID号为".$pdo->lastInsertId()."<br/>";
}catch(PDOException $e){
echo $e->getMessage();
}

pdo 增删改查