zoukankan      html  css  js  c++  java
  • 使用PDO连接数据库

    PDO(PHP数据对象) 是一个轻量级的、具有兼容接口的PHP数据连接拓展,是一个PHP官方的PECL库,随PHP 5.1发布,需要PHP 5的面向对象支持,因而在更早的版本上无法使用。它所提供的数据接入抽象层,具有与具体数据库类型无关的优势,为它所支持的数据库提供统一的操作接口。目前支持多种数据库等。由于PDO是在底层实现的统一的数据库操作接口,因而利用它能够实现更高级的数据库操作,比如存储过程的调度等。

    创建数据库配置文件config.php

    <?php
    define('DB_HOST','localhost');//常量,主机名
    define('DB_USER','root');//连接数据库的用户名
    define('DB_PWD','root');//连接数据库密码
    define('DB_NAME','book');//数据库名称
    define('DB_PORT','3306');//端口号
    define('DB_TYPE','mysql');//数据库的类型
    define('DB_CHARSET','utf8');//数据库的编码格式
    define('DB_DSN',DB_TYPE.":host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET);//定义PDO的DSN
    ?>

    创建index.php文件,用于连接数据库,执行查询语句,并引入config.php文件

    <?php
    require "config.php";
    
    try{
        //连接数据库,选择数据库
        $pdo = new PDO(DB_DSN,DB_USER,DB_PWD);
    } catch (PDOException $e){
        //输出异常信息
        echo $e->getMessage();
    }
    
    $query = "select * from books where id=?";//sql语句
    $sth = $pdo->prepare($query);//准备执行
    $sth->execute(array(1));//执行查询语句,并返回结果集
    
    //var_dump($sth->fetchColumn(1));
    //var_dump($sth->fetchColumn(1));
    //$res = $sth->fetch(PDO::FETCH_OBJ);
    include("lists_02.html");

    创建list.html文件,显示查询信息。

    <!DOCTYPE html>
    <html lang="en" class="is-centered is-bold">
    <head>
        <meta charset="UTF-8">
        <title>连接数据库</title>
        <link href="css/bootstrap.css" rel="stylesheet">
        <style>
            #name,#id{
                width: 200px;
                margin-top: 10px;
            }
        </style>
    </head>
    <body>
    <div class="container" style="padding-top: 20px">
        <div class="col-sm-offset-2 col-sm-8">
            <div class="panel panel-default">
                <div class="panel-heading">
                    图书列表
                </div>
                <div class="panel-body">
                    <table class="table table-striped task-table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>书姓</th>
                                <th>作者</th>
                                <th>价格</th>
                                <th>出版日期</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <!--$getData数组的值也是数组-->
                            <?php while ($res = $sth->fetch(PDO::FETCH_OBJ)){ ?>
                            <tr>
                                <td class="table-text">
                                    <?php echo $res->id ?>
                                </td>
                                <td class="table-text">
                                    <?php echo $res->name ?>
                                </td>
                                <td class="table-text">
                                    <?php echo $res->author ?>
                                </td>
                                <td class="table-text">
                                    <?php echo $res->price ?>
                                </td>
                                <td class="table-text">
                                    <?php echo $res->publishDate ?>
                                </td>
                                <td class="table-text">
                                    <button type="button" class="btn btn-primary">编辑</button>
                                    <button type="button" class="btn btn-danger">删除</button>
                                </td>
                            </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
    
            </div>
        </div>
    </div>
    <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    </body>
    </html>
  • 相关阅读:
    Ucenter会员整合
    站点收藏
    天天团购整理笔记一
    MSSQLSERVER 时间差计算
    shell 编程实例练习(一)
    Linux PCI 设备驱动基本框架(二)
    Shell grep / sed 学习
    转:Python自省(反射)指南
    Shell 数组与双括号
    Python基本数据生成
  • 原文地址:https://www.cnblogs.com/bushui/p/11519205.html
Copyright © 2011-2022 走看看