logo
关于我们

技术分享

技术分享 使用PDO连接数据库

使用PDO连接数据库

2021-06-22

      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文件

<?phprequire "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>

 

云祺备份软件,云祺容灾备份系统,虚拟机备份,数据库备份,文件备份,实时备份,勒索软件,美国,图书馆
  • 标签:
  • 网络安全

您可能感兴趣的新闻 换一批

现在下载,可享30天免费试用

立即下载