<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>testing</title>
</head>
<body>
<?php
/*
* 建立資料庫
* CREATE DATABASE `kiang_cycu` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
*
* 建立資料表
 
CREATE TABLE IF NOT EXISTS `messages` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`poster` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`title` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL,
`body` text COLLATE utf8_unicode_ci NOT NULL,
`time` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
 
*/
 
/*
* 資料庫連線
*/
$dbn = new mysqli(
'localhost', //主機位置
'root', //帳號
'123', //密碼
'guestbook' //資料庫名稱
);
$dbn->set_charset('utf8');
 
/*
* 新增資料
*/
if($dbn->query('SELECT COUNT(*) AS count FROM messages')->fetch_object()->count == 0) {
$now = mktime();
for($i = 1; $i <= 20; $i++) {
$stmt = $dbn->prepare('INSERT INTO messages VALUES ( ?, ?, ?, ?)');
$poster = 'poster' . $i;
$title = 'title' . $i;
$body = 'body' . $i;
$date = date('Y-m-d H:i:s', ++$now);
$stmt->bind_param('ssss', $poster, $title, $body, $date);
$stmt->execute();
$stmt->close();
}
}
 
/*
* 取得資料
*/
echo '<table>';
echo '<tr><th>編號</th><th>發表人</th><th>標題</th><th>內容</th><th>時間</th></tr>';
$result = $dbn->query('SELECT * FROM messages');
while($data = $result->fetch_object()) {
echo '<tr>';
echo '<td>' . $data->id . '</td>';
echo '<td>' . $data->poster . '</td>';
echo '<td>' . $data->title . '</td>';
echo '<td>' . $data->body . '</td>';
echo '<td>' . $data->time . '</td>';
echo '</tr>';
}
echo '</table>';
?>
</body>
</html>

cabuchi 發表在 痞客邦 PIXNET 留言(0) 人氣()