一.PDO 增删改查
连接 $pdo= New PDO ('mysql:host=localhost;dbname=houtaiablyycnbak','root','Aa122' );
接下来增删改查
$sql = "select * from patient_13 where age between 20 and 30 && sex ='女' order by age desc"; $sq="insert into patient_13(age,name,sex) Values(33,'张小姐酒','女')"; $sql="delete from patient_13 where name='彭昌雲'"; $sql="update patient_13 set name = '李值'where name='李亚飞'";
执行语句
$a=$pdo->exec($sql);
查询语句
$smt = $pdo -> query($sql); $arr = $smt -> fetchAll(pdo::FETCH_ASSOC); foreach ($arr as $arrs) { echo '<tr>'; echo "<td>{$arrs['age']} </td>"; echo "<td>{$arrs['name']}</td>"; echo "<td>{$arrs['sex'] }</td>"; echo "<br>"; echo "</tr>"; }
2.PDO 预处理
查询:
$smt=$pdo->prepare($sql); $smt->execute(); $rows =$smt ->fetchAll(PDO::FETCH_ASSOC);
$tot=$smt->rowcount(); 条数 echo "<PRE>"; print_r($rows)
增删改:
$smt=$pdo->prepare($sql); $smt->execute();
总结:
PDO 先连接》
增删改
$pdo->exec($sql);
查询:
$smt=$pdo->query($sql)
$arr = $smt -> fetchAll(pdo::FETCH_ASSOC); 然后用foreach 遍历
预处理
$smt=$pdo->prepare($sql);
$smt->execute();
$arr =$smt ->fetchAll(PDO::FETCH_ASSOC);
字段替换
$sql="UPDATE patient_18 SET name = REPLACE(name,'女士','女士修改成功') ";
姓李 单名, 电话不为空 trim()
$sql="select * from patient_18 WHERE name like '李_' and trim(tel) != '' ";
select * from patient_18 where age = (select max(age) from patient_18 )
上个月时间戳
$premonth=$month-1; $preyears=$years; if ($premonth<1) { $premonth=12; $preyears=$years-1; }
$now2=strtotime("{$preyears}-{$premonth}-1"); 上个月时间戳生成
1. 默认查询全部:
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
在下面执行的时候 就默认 查询全部
2. bindValue 定义变量查询:
$sql="select * from patient_18 where id > ? and id < ? "; $smt=$pdo->prepare($sql); $smt->bindValue(1,101); $smt->bindValue(2,105); $smt->execute(); $arr=$smt->fetchAll();
3. 预处理 增删改查总结:
$sql="INSERT INTO patient_18 (name,tel,age) VALUES('测试','13647028603','2019') "; $sql="DELETE FROM patient_18 WHERE name = '测试' and age ='1100' "; $sql="UPDATE patient_18 SET name = '测试修改' WHERE age = 2019 "; $sql="select * from patient_18 where tel= 13647028603 ";
预处理准备
$smt=$pdo->prepare($sql);
执行
$smt->execute();
结果
$arr=$smt->fetchAll();