<?php header("content-type:text/html;charset=utf-8"); $dsn="mysql:dbname=EC_ERP_v2;host=127.0.0.1"; $db_user='WebDev'; $db_pass='WebDev'; try{ $pdo=new PDO($dsn,$db_user,$db_pass); }catch(PDOException $e){ echo '数据库连接失败'.$e->getMessage(); } $openid = '94782645F82DCACFa'; $sql="select * from erp_user where user_qq='$openid'; "; $rmt=$pdo->query($sql); $res = $rmt->fetchAll(); if(count($res)>0){ echo '用户存在'; echo '<pre>'; var_dump($res); echo '</pre>'; }else{ $username = 'admin'; $userpsw = md5('123456'); $userheadurl ='http://q.qlogo.cn/qqapp/1404053/94782645F82DCACF41BF9246AEF1'; $time = time(); $sqla = "insert into erp_user (user_name,user_pwd,user_headurl,user_qq,user_createtime) values ('$username','$userpsw','$userheadurl','$openid','$time');"; $rest = $pdo->exec($sqla); $aa =$pdo->errorInfo();//可以打印错误信息; var_dump($aa); echo '影响行数:'.$rest; } //新增 $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'].'<br/>'; } //删除 $sql="delete from buyer where id>5"; $res=$pdo->exec($sql); echo '影响行数:'.$res; ?>
关于使用PDO连接数据库的一些基本操作
最新推荐文章于 2021-01-28 11:34:59 发布