发布网友 发布时间:2022-04-30 21:01
共3个回答
懂视网 时间:2022-05-01 01:22
<!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连接数据库练习(待修)
标签:
热心网友 时间:2022-04-30 22:30
错误代码:1045是数据库登录失败热心网友 时间:2022-04-30 23:48
同问怎么解决?我用的是WAMP。楼主解决了没有?