使用PHP和mysql建立一個ShoutBox

來源:互聯網
上載者:User

作為一個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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.