PDO 简单应用

//PDO 链接数据库
try{
$pdo = new PDO('mysql:host=localhost;port=3306;dbname=currency', 'root', 'root');
}catch(PDOException $e){
echo '数据库连接失败'.$e->getMessage();
}

/* //新增
$sql="insert into buyer (username,password,email) values ('ff','123456','admin@admin.com')";
$res=$pdo->exec($sql);
echo '影响行数:'.$res;

//修改
$sql="update buyer set username='ff123' where id>3";
$res=$pdo->exec($sql);
echo '影响行数:'.$res;

//查询
$sql="select * from buyer";
$res=$pdo->query($sql);
foreach($res as $row){
echo $row['username'].'
';
}

//删除
$sql="delete from buyer where id>5"; */

//占位符使用
$id = 1;
$b=$pdo->prepare("select * from yang_member where member_id = :id;");
$b->bindParam(":id",$id);
$b->execute();

$b->fetchAll(PDO::FETCH_ASSOC); //获取所有数据
$b->fetch(PDO::FETCH_ASSOC); //获取一条数据

标签: none

添加新评论