• PDO连接数据库


    本例是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>

     查询表某个字段,并存入session方法:

    1 $sql="select * from tb_user where regname=? and regpwd=?";
    2         $result=$pdo->prepare($sql);
    3         $result->execute(array($username,$pwd));
    4         $res=$result->fetch(PDO::FETCH_ASSOC);
    5         if($res!=""){
    6             $_SESSION["username"]=$username;
    7             $_SESSION["pwd"]=$pwd;        
    8             $_SESSION["turename"]=$res['regrealname'];
    9             echo "<script>
  • 相关阅读:
    simple-LDAP-auth
    User Attributes
    webpack 模块标识符(Module Identifiers)
    详解webpack中的hash、chunkhash、contenthash区别
    [转] 插件兼容CommonJS, AMD, CMD 和 原生 JS
    Exif.js 读取图像的元数据
    [转] 跨域
    [转] 理解Web路由
    [转] React 是什么
    [转] Web MVC简介
  • 原文地址:https://www.cnblogs.com/mrcln/p/3724414.html
Copyright © 2020-2023  润新知