当前位置:Gxlcms > 数据库问题 > PDO连接数据库练习(待修)

PDO连接数据库练习(待修)

时间:2021-07-01 10:21:17 帮助过:6人阅读

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title>无标题文档</title> 6 </head> 7 8 <body> 9 <?php 10 11 try 12 { 13 $dsn = "mysql:dbname=mydb;host=localhost"; //mysql 是驱动名称 dbname数据库名 host服务器地址 14 15 $pdo = new PDO($dsn,"root","123");      //root数据库用户名 123数据库密码 16 17 } 18 catch(PDOException $e) 19 { 20 echo "连接出错".$e->getMessage(); 21 } 22 23 //查询 24 $sql = "select * from Info"; 25 26 $stmt = $pdo->prepare($sql); 27 28 $stmt->execute(); 29 30 while($row = $stmt->fetch(PDO::FETCH_NUM)) 31 { 32 print_r($row); 33 echo "<br>"; 34 } 35 36 37 //添加 38 $sqltj = " insert into info values(:code,:name,:nation,:sex,:birthday)" ; 39 40 $sttj =$pdo->prepare($sqltj); 41 42 $sttj->execute(array("code"=>"p009","name"=>"王五","nation"=>"苗族","sex"=>0,"birthday"=>"1995-09-06")); 43 44 45 ?> 46 </body> 47 </html>

网页:        

技术分享

数据库:

技术分享

PDO连接数据库练习(待修)

标签:

人气教程排行