如何用PHP和mysql创建一个ShoutBox
- 作者:williamlong 来源:月光博客 文章点击数:
作为一个PHP开发人员,我有时被要求作个shoutbox 。 如果同样的事情也发生在你身上,这里有一个快速指南。显然,您要为它添加您自己的CSS在上面,但这里是基本思路。我们需要一个MySQL数据库表和三个PHP文件。
首先,我们需要一个文件保存数据库信息
--- 文件 #1: mysql.inc.php ---
<?php
# Simply Shouting - a shoutbox example
# 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 - a shoutbox example
# 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 - a shoutbox example
# 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.php automatically, but then they'd wonder if it created correctly or not. -->
点击 <a href="index.php">这里</a>开始.</html>
- 相关文章
| ·关于PHP在linux下面获取GOOGLE的PR值 | 07-15 |
| ·PHP程序不适用大型系统的九大原因 | 07-15 |
| ·Mysql使用Describe命令判断字段是否存在 | 07-12 |
| ·MySQL UPDATE时主键冲突引发的思考 | 07-12 |
| ·使用PHP模拟HTTP认证 | 07-10 |
| ·分享是Wodig开源的精髓 | 07-10 |
| ·从IIS的ASP迁移到APACHE的PHP的方法 | 07-07 |
| ·ASP、PHP和JSP的比较 | 07-02 |
