PDO (PHP Data Objects) is a lightweight, compatible with a data connection interface PHP development, PHP is an official of the PECL repository, with PHP 5.1 release, you need to support object-oriented PHP 5, so the earlier version Not available. It provides data access abstraction layer, has nothing to do with the specific database type, will provide a unified user interface for the database it supports. Currently supports a variety of databases. Since PDO is a unified interface to the underlying implementation of the database operation, so it is possible to use more advanced database operations, such as scheduling a stored procedure.
Create a database configuration file config.php
<? PHP
DEFINE ( ' DB_HOST ' , ' localhost ' ); // constant, hostname
DEFINE ( ' DB_USER ' , ' the root ' ); // User name database connected
DEFINE ( ' DB_PWD ' , ' the root ' ); / / connect to the database password
DEFINE ( ' the DB_NAME ' , ' Book ' ); // database name
DEFINE ( ' DB_PORT ' , '3306' ); // port number
DEFINE ( ' db_type ' , ' MySQL ' ); // type database
DEFINE ( ' db_charset ' , ' UTF8 ' ); // encoding format database
DEFINE ( ' DB_DSN ' , db_type. " : = Host " .DB_HOST. " ; dbname = " .DB_NAME. " ; charset = " .DB_CHARSET); // define the PDO DSN
>?
Create a index.php file, connect to a database, execute a query, and the introduction of the config.php file
<? PHP
the require "the config.php" ;
the try {
// connect to the database, select the database
$ PDO = new new the PDO (DB_DSN, DB_USER, DB_PWD);
} the catch (PDOException $ E ) {
// output abnormality information
echo $ E -> getMessage ();
}
$ Query = "Books from the SELECT * the WHERE the above mentioned id =?"; // SQL statement
$ STH = $ PDO -> pREPARE ( $ Query ); // ready to execute
$ STH -> the execute ( Array (1) ); // execute a query and returns the result set
//var_dump($sth->fetchColumn(1));
//var_dump($sth->fetchColumn(1));
//$res = $sth->fetch(PDO::FETCH_OBJ);
include("lists_02.html");
Creating list.html file, display query information.
<!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 > book named </ TH >
< TH > Author </ TH >
< TH > Price </ TH >
< TH > Publication Date </ TH >
< TH > operation </ 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>