# 1.新建data_v3/v1/view-post.php文件
~~~
<?php
/**
* Emai:y7wanghui@gmai.com
* Date: 2020/11/2 0002
*/
// htmlspecialchars 将特殊字符转换为 HTML 实体
// 找出数据库的路径,以便SQLite / PDO可以连接
$root = __DIR__;
$database = $root . '/../data/data.sqlite';
$dsn = 'sqlite:' . $database;
//接收index.php传过来的id
if (isset($_GET['post_id'])){
$postId = $_GET['post_id'];
}else{
//总是定义一个id
$postId = 0;
}
// 连接到数据库,运行查询,处理错误
$pdo = new PDO($dsn);
$stmt = $pdo->prepare(
'SELECT
title, created_at, body
FROM
post
WHERE
id = :id'
);
if ($stmt === false)
{
throw new Exception('准备此查询时出现问题');
}
$result = $stmt->execute(
array( 'id'=> $postId,)
);
if ($result === false)
{
throw new Exception('运行此查询时出现问题');
}
$row = $stmt->fetch(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
<title>
我的博客 |
<?php echo htmlspecialchars($row['title'], ENT_HTML5, 'UTF-8') ?>
</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<h1>标题</h1>
<h2>
<?php echo htmlspecialchars($row['title'], ENT_HTML5, 'UTF-8') ?>
</h2>
<div>
<?php echo $row['created_at'] ?>
</div>
<p>
<?php echo htmlspecialchars($row['body'], ENT_HTML5, 'UTF-8') ?>
</p>
</body>
</html>
~~~