作為一個PHP開發人員,我有時被要求作個shoutbox。如果同樣的事情也發生在你身上,這裡有一個快速指南。顯然,您要為它添加您自己的CSS在上面,但這裡是基本思路。
我們需要一個MySQL資料庫表和三個PHP檔案。
首先,我們需要一個檔案儲存資料庫資訊
--- 檔案 #1:mysql.inc.php---
<?php
# Simply Shouting - ashoutboxexample
# File name:mysql.inc.php
# Description: A file to hold database info.
$host ='localhost';
$user ='database_user_name';
$password='database_user_password';
$name ='database_name';
?>
建立一個有四個欄位的資料表. 我們命名為shouts. 此前可能你沒有這個SQL檔案, 建立一個PHP檔案"install.php". 這個檔案用過一次之後,記得要刪除它!
-- 檔案 #2: install.php--
<?php
# Simply Shouting - ashoutboxexample
# File name: install.php
# Description: Creates the database table.
// include the database info file
include("mysql.inc.php");
//串連資料庫
$connection= @mysql_connect($host,$user,$password) or die(mysql_error());
$db= @mysql_select_db($name,$connection) or die(mysql_error());
//如果我們已經有一個表名字叫做"shouts", 需要先刪除它
$sql='DROP TABLE IF EXISTS `shouts`';
$result= @mysql_query($sql,$connection) or die(mysql_error());
// 現在確定沒有相同名字的表, 建立它
$sql='CREATE TABLE `shouts` (
`id` int(11) NOT NULL auto_increment,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`shoutby` varchar(50) default NULL,
`shout` varchar(50) default NULL,
PRIMARY KEY `id` (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1';
echo'Creating table: \'shouts\'....';
// 關閉串連
$result= @mysql_query($sql,$connection) or die(mysql_error());?>
<html>
<head>
<title>Simply Shouting - 安裝</title>
</head>
<body>
<br />
你的安裝過程已經完成. 請立即從你的伺服器上刪除所有安裝檔案. 本程式包含以下安裝檔案:<br />
<br />
1) install.php<br />
<br />
<br />
<!-- I could just send them to index.phpautomatically, but then they'd wonder if it created correctly or not. -->
點擊 <a href="index.php">這裡</a>開始.</html>