pdo操作数据库


1.连接数据库

/* 通过调用驱动程序创建一个PDO实例 */
$dsn = 'mysql:dbname=lv;host=127.0.0.1';
$user = 'root';
$password = 'root';
try {
    $pdo = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

2.查询数据库

/* 通过调用驱动程序创建一个PDO实例 */
$dsn = 'mysql:dbname=lv;host=127.0.0.1';
$user = 'root';
$password = 'root';
try {
    $pdo = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
$sql = "insert into aaaaa ('name','email') values ('xiaoming','1647114924@qq.com')";
$select = "select * from aaaaa where id=1";
$prepare = $pdo->prepare($select);
$request = $prepare->execute();
$a = $prepare->fetch(PDO::FETCH_ASSOC);
var_dump($a);

pdo操作数据库

 

 3.删除一条数据

$delete = "delete from bbb where id=1";
$ac = $pdo->exec($delete);
if($ac){
    echo "删除成功";
}else{
    echo "删除失败";
}

pdo操作数据库

 

 

4.修改一条数据

$update = "update bbb set name='daming',email='aaa' where id=1";
$ss = $pdo->exec($update);
if($ss){
    echo "修改成功";
}else{
    echo "修改失败";
}

pdo操作数据库

 

 5.增加一条数据

$sql = "insert into bbb (user_name,user_gender) values ('aa44','vv66')";
$pp=$pdo->exec($sql);
if($pp){
    echo "添加成功";
}else{
    echo "添加失败";

6.连接数据库 (增,删,改,查)

/* 通过调用驱动程序创建一个PDO实例 */
$dsn = 'mysql:dbname=lv;host=127.0.0.1';
$user = 'root';
$password = 'root';
try {
    $pdo = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
//添加一条数据
$sql = "insert into bbb (user_name,user_gender) values ('aa44','vv66')";
$pp=$pdo->exec($sql);
if($pp){
    echo "添加成功";
}else{
    echo "添加失败";
}
//查询一条数据
$select = "select * from bbb where id=2";
$prepare = $pdo->prepare($select);
$request = $prepare->execute();
$a = $prepare->fetch(PDO::FETCH_ASSOC);
var_dump($a);

//更新一条数据
$update = "update bbb set name='daming',email='aaa' where id=1";
$ss = $pdo->exec($update);
if($ss){
    echo "修改成功";
}else{
    echo "修改失败";
}

//删除一条数据
$delete = "delete from bbb where id=1";
$ac = $pdo->exec($delete);
if($ac){
    echo "删除成功";
}else{
    echo "删除失败";
}

 

原创文章,作者:kepupublish,如若转载,请注明出处:https://blog.ytso.com/277887.html

(0)
上一篇 2022年7月30日
下一篇 2022年7月30日

相关推荐

发表回复

登录后才能评论