Skip to content

Commit

Permalink
示例完成
Browse files Browse the repository at this point in the history
  • Loading branch information
Ye-Ting committed Jul 14, 2015
1 parent f8735c7 commit a854d47
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 25 deletions.
79 changes: 79 additions & 0 deletions DB.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Created by PhpStorm.
* User: yeting
* Date: 15/7/14
* Time: 下午12:21
*/
require('Helper.php');

class DB
{
protected $pdo;

function __construct()
{
$serverName = env("DB_HOST", "localhost");
$databaseName = env("DB_DATABASE", "homestead");
$username = env("DB_USERNAME", "homestead");
$password = env("DB_PASSWORD", "secret");

try {
$this->pdo = new PDO("mysql:host=$serverName;dbname=$databaseName", $username, $password);

// set the PDO error mode to exception
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// 检测数据库是否存在表
$isInstall = $this->pdo->query("SHOW TABLES like 'contacts';")
->rowCount();

// sql to create table
if (!$isInstall) {
$sql = "
CREATE TABLE contacts (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
phone VARCHAR(255) NOT NULL )
";
$this->pdo->exec($sql);

$sqlData = "
INSERT INTO `contacts` VALUES ('1', 'John', '188888888');
INSERT INTO `contacts` VALUES ('2', 'Bob', '166666666');
INSERT INTO `contacts` VALUES ('3', 'Zoe', '155555555');
";
$this->pdo->exec($sqlData);
}


} catch (PDOException $e) {
echo "数据库链接失败: " . $e->getMessage();
die();
}
}

public function all()
{
return $this->pdo->query('SELECT * from contacts')
->fetchAll();
}

public function find($id)
{
return $this->pdo->query("SELECT * from contacts WHERE id = $id ")
->fetch();
}

public function remove($id)
{
return $this->pdo->exec("DELETE from contacts WHERE id = $id ");
}

public function add($name, $phone)
{
$sql = "INSERT INTO contacts ( name , phone ) VALUES ($name,$phone)";

return $this->pdo->exec($sql);
}
}
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "DaoCloud/PHP-Apache-Mysql-Sample",
"require": {
"php": ">=5.3.0"
}
}
27 changes: 27 additions & 0 deletions helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* 获取环境变量
* @param $key
* @param null $default
* @return null|string
*/
function env($key, $default = null)
{
$value = getenv($key);

if ($value === false) {
return $default;
}

return $value;
}

/**
* @param $data
*/
function dd($data)
{
var_dump($data);
die();
}
115 changes: 90 additions & 25 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,99 @@
* Date: 15/7/9
* Time: 下午4:10
*/
$serverName = env("DB_HOST", "localhost");
$databaseName = env("DB_DATABASE", "homestead");
$username = env("DB_USERNAME", "homestead");
$password = env("DB_PASSWORD", "secret");

require('DB.php');

try {
$conn = new PDO("mysql:host=$serverName;dbname=$databaseName", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db = new DB();

echo "数据库链接成功";
} catch (PDOException $e) {
echo "数据库链接失败: " . $e->getMessage();

if ($_POST) {
$db->add($_POST['name'], $_POST['phone']);
}
/**
* 获取环境变量
* @param $key
* @param null $default
* @return null|string
*/
function env($key, $default = null)
{
$value = getenv($key);
if ($_GET['delete']) {
$db->remove($_GET['delete']);
}

$contacts = $db->all();
?>

<html>
<head>
<title>PHP-Apache-Mysql-Sample 示例 - DaoCloud</title>

<link href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">

<style>
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
color: #666;
display: table;
/*font-weight: 100;*/
}

.container {
display: table-cell;
text-align: center;
/*vertical-align: middle;*/
}

.content {
text-align: center;
display: inline-block;
}


if ($value === false) {
return $default;
}
</style>
</head>
<body>
<div class="container">
<div class="content">
<h1>
PHP-Apache-Mysql-Sample 示例
</h1>
<table class="table">
<caption>通讯录</caption>
<thead>
<tr>
<th>#</th>
<th>姓名</th>
<th>电话</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php foreach ($contacts as $index => $contact) {
?>
<tr>
<th scope="row"> <?php echo $index + 1 ?></th>
<td><?php echo $contact['name'] ?></td>
<td><?php echo $contact['phone'] ?></td>
<td>
<a href="index.php?delete=<?php echo $contact['id'] ?>">删除</a>
</td>
</tr>
<?php
} ?>
</tbody>
</table>

return $value;
}
<div class="text-left">
<form method="post">
<div class="form-group">
<label for="name">姓名</label>
<input type="text" name="name" class="form-control" id="name" placeholder="姓名">
</div>
<div class="form-group">
<label for="phone">号码</label>
<input type="text" name="phone" class="form-control" id="phone" placeholder="号码">
</div>
<button type="submit" class="btn btn-success">新增</button>
</form>
</div>
</div>
</div>
</body>
</html>

0 comments on commit a854d47

Please sign in to comment.