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

PDO连接数据库

时间:2014-05-13 18:24:02      阅读:366      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   c   

本例是PDO连接Mysql数据库的方法:

连接别的数据库的方法大同小异,只需在php.ini文件中加载“extension=php_pdo.dll“和”extension=php_pdo_mysql.dll(根据需求加载对应的dll文件)“,保存重启服务器即可。

以下是本例代码:

bubuko.com,布布扣
 1 <table border="1">
 2     <tr>
 3         <td>id</td>
 4         <td>fileid</td>
 5         <td>username</td>
 6         <td>content</td>
 7         <td>datetime</td>
 8     </tr>
 9 <?php
10     header("Content-Type:text/html;charset=utf-8");
11     $dbms=‘mysql‘;
12     $dbname=‘db_tmlog‘;
13     $user=‘root‘;
14     $pwd=‘‘;
15     $host=‘localhost‘;
16     $dsn="$dbms:host=$host;dbname=$dbname";
17     mysql_query("set names utf8");
18     echo "PDO测试:";
19     try{
20         $pdo=new PDO($dsn,$user,$pwd);
21         echo "PDO连接Mysql成功!";
22         $query = "select * from tb_filecomment where username like ?";
23         $result = $pdo->prepare($query);
24         $result->execute(array(‘%s%‘));
25         while($res=$result->fetch(PDO::FETCH_ASSOC)){
26 ?>
27     <tr>
28         <td><?php echo $res[‘id‘];?></td>
29         <td><?php echo $res[‘fileid‘];?></td>
30         <td><?php echo $res[‘username‘];?></td>
31         <td><?php echo $res[‘content‘];?></td>
32         <td><?php echo $res[‘datetime‘];?></td>
33     </tr>
34 <?php
35         }
36     }catch(Exception $e){
37         echo $e->getMessage();
38     }
39 
40 ?>
41 </table>
bubuko.com,布布扣

 

PDO连接数据库,布布扣,bubuko.com

PDO连接数据库

标签:style   blog   class   code   java   c   

原文地址:http://www.cnblogs.com/mrcln/p/3724414.html

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