PHP中pdo的使用

<?php
/**
*下面代码中information为表名
*
*/
//1.先要连数据库
$pdo=new PDO('mysql:host=localhost;dbname=数据库名','用户','密码');
// $pdo=new PDO('mysql:host=localhost;dbname=数据库名','一般是root','一般为空不填');对于新手不会配置的
//query语句的查询
// $state=$pdo->query('select * from information');
// $stmt=$state->fetchAll(PDO::FETCH_ASSOC);
// var_dump($stmt);


//exec语句的增
// $affected=$pdo->exec('insert into information (name,sex) values("pp",15)');
// var_dump($affected);

//改
// $res=$pdo->exec('update information set name="pps" where name="pp"');

//删
// $res=$pdo->exec('delete from information where name="pps"');
// var_dump($res);

//prepare语句的增
// $stmt=$pdo->prepare('insert into information (name,sex) values(:name,:sex)');
// $arr=array(
// 'name'=>"pipixia",
// 'sex'=>'20'
// );
// $res=$stmt->execute($arr);

//改
// $stmt=$pdo->prepare('update information set name = :name where id = :id');
// $arr=array(
// 'name'=>"pps",
// 'id'=>10
// );
// $res=$stmt->execute($arr);


//删
// $stmt=$pdo->prepare('delete from information where id=:id');
// $arr=array(
// 'id'=>10
// );
// $res=$stmt->execute($arr);

//查
// $query = $pdo->prepare("select * from information");
// $query->execute();
// $row = $query->fetchAll(PDO::FETCH_ASSOC);
// var_dump($row);

原文地址:https://www.cnblogs.com/dyj--php/p/7842892.html