Correction status:qualified
Teacher's comments:将一个大项目, 进行拆分, 然后再组合起来是很常用的方式
连接数据库connect.php
<?php //1.连接数据库 if ($_SERVER['HTTP_HOST']=='html.io') { $db =[ 'type'=>'mysql', 'host'=>'127.0.0.1', 'dbname'=>'ouyangke', 'username'=>'root', 'password'=>'root' ]; } else{ $db =[ 'type'=>'mysql', 'host'=>'127.0.0.1', 'dbname'=>'zhulaoshi', 'username'=>'root', 'password'=>'root' ]; }; $dsn=$db['type'].':host='.$db['host'].';dbname='.$db['dbname']; try{ $pdo=new PDO($dsn,$db['username'],$db['password']); } catch(PDOException $e){ //输出,停止功能 die( $e-> getMessage()); }; ?>
点击 "运行实例" 按钮查看在线实例
2.获取数据,放置公共资源:header.php
<?php // 1.连接数据库:$pdo require 'connect.php'; //执行SQL语句完成数据库操作 $sql = 'SELECT * FROM `system` LIMIT 1'; //创建SQL语句对象 $stmt = $pdo->prepare($sql); //执行SQL查询 $stmt->execute(); //创建一个变量让数据库的数据保存,这个是system $system = $stmt->fetch(PDO::FETCH_ASSOC); // 获取栏目信息 $sql = 'SELECT `cate_id`,`name`,`alias` FROM `category`'; $stmt = $pdo->prepare($sql); $stmt->execute(); $cates = $stmt->fetchAll(PDO::FETCH_ASSOC); $cate_count = count($cates); // 栏目数量 // 获取影视信息 $sql = 'SELECT `mov_id`,`name`,`image`, `detail`, `cate_id` FROM `movies`'; $stmt = $pdo->prepare($sql); $stmt->execute(); $movies = $stmt->fetchAll(PDO::FETCH_ASSOC); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="/0926/static/css/style.css"> <meta name="description" content="<?php echo $system['desc'] ?>"> <meta name="keywords" content="<?php echo $system['key'] ?>"> <title><?php echo $system['title'] ?></title> </head> <body> <!--头部导航--> <div class="header"> <ul class="nav"> <li><a href="index.php">首页</a></li> <?php foreach ($cates as $cate) : ?> <li><a href="list.php?cate_id=<?php echo $cate['cate_id']; ?>"><?php echo $cate['alias'] ?></a></li> <?php endforeach; ?> </ul> </div>
点击 "运行实例" 按钮查看在线实例
3. 网站首页部分:index.php
<?php include __DIR__ . "/header.php"; foreach ($cates as $cate) { echo "<h2>{$cate['alias']}</h2>"; echo '<ol>'; foreach ($movies as $movie) { if ($cate['cate_id'] === $movie['cate_id']) { echo "<li><a href='detail.php?mov_id=" .$movie['mov_id']. "'>{$movie['name']}</a></li>"; } } echo '</ol>'; } include __DIR__ . '/footer.php'; ?>
点击 "运行实例" 按钮查看在线实例
4.分类页面:list.php
<?php include __DIR__ . '/header.php'; // 获取栏目ID // 注: 数据表读取的值,都是以字符类型,这里不必再转换 $cate_id = $_GET['cate_id']; foreach ($cates as $cate) { if ($cate['cate_id'] === $cate_id) { echo "<h2>{$cate['alias']}</h2>"; echo '<ol>'; // 遍历该栏目ID对应的影视信息 foreach ($movies as $movie) { if ($cate['cate_id'] === $movie['cate_id']) { echo "<li><a href='detail.php?mov_id=" .$movie['mov_id']. "'>{$movie['name']}</a></li>"; } } echo '</ol>'; }; } include __DIR__ . '/footer.php'; ?>
点击 "运行实例" 按钮查看在线实例
5.项目详情页面:detail.php
<?php include __DIR__ . '/header.php'; $mov_id = $_GET['mov_id']; foreach ($movies as $movie) { if ($movie['mov_id'] === $mov_id) { echo "<h3>{$movie['name']}</h3>"; echo '<img src="/0926/static/images/'.$movie['image'].'" alt="" width="300">'; echo "<p style='text-indent: 2em'>{$movie['detail']}</p>"; } }; include __DIR__ . '/footer.php'; ?>
点击 "运行实例" 按钮查看在线实例
6.添加网站底部公共资源页面:footer.php
<!--底部版权--> <div class="footer"> <p class="copyright"><?php echo $system['copy']; ?>© 版权所有</p> </div> </body> </html>
点击 "运行实例" 按钮查看在线实例
总结:大概了解里面的关系,如何连接数据库,拿出数据填充网页动态数据交互!
必须要学习牢记: 获取数据库方法的使用点
PDO 绑定参数
bindParam(): 将参数绑定到SQL语句模板上
常用的预定义常量:
PDO::PARAM_INT: 整数类型
PDO::PARAM_STR: 字符串类型
PDO::FETCH_ASSOC: 获取结果集中的关联部分
PDOStatement 类常用方法
execute(): 执行SQL语句(读/写)
rowCount(): 返回受影响的记录数量(不适合SELECT)
errorInfo(): 返回错误信息数组
fetch(): 获取结果集中的下一行
fetchAll(): 返回结果集中的所有行
fetchColumn(): 返回结果集下一行的单独的一列
bindColumn(): 将结果集某字段绑定到指定变量上