如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
增删改
//连接数据库 $pdo = new PDO('mysql:host=localhost;dbname=user','root','root'); //设置字符集 $pdo->query('set names utf8'); //添加 //$sql = "insert into info (name,sex,age)values('CC','女','18')"; //$rows = $pdo->exec($sql);//影响行数 //$id = $pdo->lastInsertId();//返回自增id //删除 //$sql = "delete from info where id = 51 "; //$rows = $pdo->exec($sql); //修改 $sql = "update info set name='vv' where id=42"; $rows = $pdo->exec($sql); if(!$rows){ echo "失败";die; }
查
//连接数据库 $pdo = new PDO('mysql:host=localhost;dbname=user','root','root'); //设置字符集 $pdo->query('set names utf8'); //sql语句 $sql = "select * from info"; $result = $pdo->query($sql); $list = $result->fetch(PDO::FETCH_BOTH);//返回单条 $lists = $result->fetchAll(PDO::FETCH_ASSOC);//返回多条 //FETCH_ASSOC 关联数组 //FETCH_NUM 索引数组 //FETCH_BOTH 混合数组