码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2016-04-01 01:07:06      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

 1 <!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连接数据库练习(待修)

标签:

原文地址:http://www.cnblogs.com/sihuiming/p/5343376.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!