当前位置:Gxlcms > 数据库问题 > 使用PDO连接数据库

使用PDO连接数据库

时间:2021-07-01 10:21:17 帮助过:3人阅读

 

创建数据库配置文件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>

 

使用PDO连接数据库

标签:hda   local   select   etc   task   需要   script   lan   center   

人气教程排行