本例是PDO连接Mysql数据库的方法:
连接别的数据库的方法大同小异,只需在php.ini文件中加载“extension=php_pdo.dll“和”extension=php_pdo_mysql.dll(根据需求加载对应的dll文件)“,保存重启服务器即可。
以下是本例代码:
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>