PHP和MySQL Web开发从新手到高手,第4天-搭建基本框架

1. 添加首页index.php,最终郊果 如图:

image

1.1 编辑首页内容

  image

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Joke Management System</title>
</head>
<body>  
  <h1>Joke Management System</h1>
  <ul>
    <li><a href="jokes/">Manage Jokes</a></li>
    <li><a href="authors/">Manage Authors</a></li>
    <li><a href="categories/">Manage Joke Categories</a></li>
  </ul>
</body>
</html>

2. 搭建总体框架

image 

image

3. 框架的前期准备

3.1 定义访问数据库操作

站点中经常访问数据库,我们可以把此段代码封装起来.

include_once $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/db.inc.php';

db.inc.php文件实现,其中ijdb_user1是访问数据库的用户名,123456是密码.

<?php

  try {
      $dsn = 'mysql:host=localhost;dbname=ijdb;';
      $username = 'ijdb_user1';
      $passwd = '123456';
      
      $pdo = new PDO($dsn, $username, $passwd);
      $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      
      $statement = 'SET NAMES "utf8"';
      $pdo->exec($statement);
     
  } catch (PDOException $e) {
      $error = 'connect to database failed...'
              .$e->getMessage();
      include 'error.html.php';    
  } 
posted @ 2015-11-03 20:00  leungcnblogs  阅读(446)  评论(0编辑  收藏  举报