10. php最大的特色就是操作数据库的能力特别的强大,php提供对多种数据库的支持。
通过php你可以轻松的连接到数据库,请求数据并将其显示在你的web站点中,甚至修改数据库中的数据。在这一节里我们主要以在互联网上跟php一起使用得最多的mysql数据库为例,介绍一下相关的mysql数据库的操作函数以及数据库的基本操作等方面的知识。
在mysql数据库中,我们用来连接数据库的函数有两个,它们分别为:
integer mysql_connect(string host,string user,string password);
integer mysql_pconnect(string host,string user,string password);
mysql_connect函数和mysql_pconnect函数都是对指定主机上mysql数据库的连接,如果该数据库位于一个不同的端口,则可以在主机名后加上冒号和端口号。函数的参数也可以缺省不填,如果不填参数,默认的主机名是“localhost”,用户名为数据库管理员,默认值为“root”,密码为空。与数据库连接成功之后,这两个函数都可以返回一个连接号,如果连接失败,则返回一个false值。让我们来看看下面几句语句:
<?
$db=mysql_connect("localhost","user","password");
mysql_select_db("mydb",$db);
?>
注释:
$db=mysql_connect("localhost","user","password"); 我们将mysql的链接参数,包括主机名、用户名和密码作为mysql_connect()的参数,同时得到返回值为$db,这样,在下面的语句中,我们就可以将变量$db作为一个连接mysql数据库的连接号来使用。
mysql_select_db("mydb",$db); 将php程序链接到mydb数据库中,这样程序与数据库的链接就完成了。
10.1 一个简易的数据库留言簿
在完成数据库的链接之后,我们就可以对数据库进行一系列的操作。下面是一个简易的数据库留言簿程序(guestbook.php3):
我假设你机子上的mysql数据库以及管理mysql数据库的工具 phpmyadmin_2. 0.5都已经安装完成,并且可以正常工作。
我们要做的第一件事情是创建一个留言数据库,假定名字为: mydb。
1、启动浏览器,打开phpmyadmin_2. 0.5 的管理web界面。
2、在“create new database”文本框内输入数据库名称mydb,然后按create按键。
下一步,我们要在该留言数据库下创建一个数据表,假定名字为: guestbook。
创建该数据表的命令如下所示:
create table guestbook (id int not null auto_increment, name char(250), email char(250), job char(250), comments blob, primary key(id));
最后,将下面的留言簿程序挎贝到你机子的可写目录下面,并保存成guestbook.php3文件。就这么简单,你已经有了自己的留言簿了。
10.2 留言簿程序(guestbook.php3):
<?php
/* $host : your mysql-host, usually localhost */
/* $user : your mysql-username */
/* $password : your mysql-password */
/* $database : your mysql-database */
/* $table : your mysql-table */
/* $page_title : the title of your guestbook-pages */
/* $admin_mail : email-address of the administrator to send the new entries to */
/* $admin_name : the name of the administrator */
/* $html_mail : say yes if your mail-agent can handle html-mail, else say no */
$host = "localhost";
$user = "";
$password = "";
$database = "mydb";
$table = "guestbook";
$page_title = "pert guestbook";
$admin_mail = "pert@21cn.com";
$admin_name = "webmaster";
$html_mail = "no";
?>
<html>
<head>
<title><?php echo $page_title; ?></title>
</head>
<body bgcolor="#ffffff" link="#000000">
<font face="verdana" size="-2">
<?
/* connect to the database */
mysql_pconnect("$host","$user","$password") or die("cant connect to the sql-server");
mysql_select_db("$database");
/* action=view : retrieve data from the database and show it to the user */
if($action == "view") {
/* function for showing the data */
function search_it($name) {
/* some vars */
global $offset,$total,$lpp,$dir;
global $table,$html_mail,$admin_name,$admin_mail;
/* select the data to get out of the database */
$query = "select name, email, job, comments from $table";
$result = mysql_query($query);
$total= mysql_numrows($result);
print "<center><font face="verdana" size="-2"><a href="guestbook.php3?action=add" onmouseover="window.status=add your name;return true" onmouseout="window.status=;return true" title="add your name">加入留言</a></font></center><br><br>";
if ($total== 0) {
print "<center>此刻没人留言</center><br><br>"; }
elseif ($total> 0) {
/* default */
$counter=0;
if ($dir=="") $dir="next";
$lpp=5;
if ($offset==0) $offset=0;
if ($dir=="next") {
if ($total > $lpp) {
$counter=$offset;
$offset+=$lpp;
$num=$offset;
if ($num > $total) {
$num=$total; } }
else {
$num=$total; } }
elseif ($dir=="previous") {
if ($total > $lpp) {
$offset-=$lpp;
if ($offset < 0) {
$offset=0; }
$counter=$offset-$lpp;
if ($counter < 0)
$counter=0;
$num=$counter+$lpp; }
else {
$num=$total; } }
while ($counter < $num) {
$j=0;
$j=$counter + 1;
/* now really grab the data */
$i1=mysql_result($result,$counter,"name");
$i2=mysql_result($result,$counter,"email");
$i3=mysql_result($result,$counter,"job");
$i4=mysql_result($result,$counter,"comments");
$i4 = stripslashes ("$i4");
/* print it in a nice layout */
print "<center>n";
print "<table width=400 border=0 align=center valign=top><tr><td><font face="verdana" size="-2">n";
print "<hr>n";
print "<br><b>name:</b> $i1n";
print "<br><b>email:</b><a href="mailto:$i2" onmouseover="window.status=email $i2;return true" onmouseout="window.status=;return true" title="email $i2">$i2</a>n";
print "<br><b>job:</b> $i3n";
print "<br><b>comment:</b>n";
print "<br>$i4n";
print "</font></td></tr></table>n";
print "</center>n";
$counter++;
}
}
mysql_close();
}
/* execute the function */
search_it($name);
/* see if we need to put on the next or previous buttons */
if ($total > $lpp) {
echo("<form action="$php_script" method="post">n");
/* see if we need a previous button */
if ($offset > $lpp) {
echo("<input type="submit" value="previous" name=dir>n"); }
/* see if we need a next button */
if ($offset < $total) {
echo("<input type="submit" value="next" name=dir>n"); }
echo("<input type=hidden name="offset" value="$offset">n");
echo("<input type=hidden name="name" value="$name">n");
echo("</form>");
}
}
/* action=add : show a form where the user can enter data to add to the database */
elseif($action == "add") { ?>
<table width="460" align="center" valign="top">
<th colspan="2"><p>请您填写留言</th>
<form name="guestbook" action="guestbook.php3?action=send" method="post">
<tr>
<td align="right" valign="top">
您的大名:</td>
<td><input type=text name=name></td>
</tr>
<tr>
<td align="right" valign="top">
您的e-mail:</td>
<td>
<input type=text name=email></td>
</tr>
<tr>
<td align="right" valign="top">
您的工作:</td>
<td>
<input type=text name=job></td>
</tr>
<tr>
<td align="right" valign="top">
您的留言:</td>
<td>
<textarea name=comments cols=40 rows=6></textarea>
<p>
<input type=submit value=submit> <input type=reset value=reset>
<a align="right" href="guestbook.php3?action=view" onmouseover="window.status=read all comments first;return true" onmouseout="window.status=;return true" title="read all comments first"><font size="-2">先观看所有的留言</font></a>
</td>
</tr>
</form>
</table>
</center>
<?
}
/* action=send : add the data from the user into the database */
elseif($action == "send") {
/* check if a html-mail should be send or a plain/text mail */
if($html_mail == "yes") {
mail("$admin_name <$admin_mail>","php3 guestbook addition","<html><body><font face="century gothic"><table border="0" width="100%" cellspacing="4"><tr>$name ($email) schreef het volgende bericht in het gastenboek :</tr><tr><td align="left"> </td><td align="left" nowrap> </td></tr><tr><td align="left">$comments</td><td align="left" nowrap> </td></tr><tr><td align="left"> </td><td align="left" nowrap> </td></tr><tr><td align="left">您的留言:</td><td align="left" nowrap>$name</td></tr><tr><td align="left">您的大名:</td><td align="left" nowrap>$email</td></tr><tr><td align="left">您的email:</td><td align="left" nowrap>$job</td></tr><tr><td align="left">您的工作:</td></tr></table></body></font></html>", "from: $name <$email>nreply-to: $name <$email>ncontent-type: text/htmlnx-mailer: php/" . phpversion());
}
/* mysql really hates it when you try to put things with or " characters into a database, so strip these…*/
$comments = addslashes ("$comments");
$query = "insert into guestbook values(,$name, $email, $job, $comments)";
$result = mysql_query($query);
?>
<br><p align = center>感谢, <?php echo $name; ?>, 您的留言.
<br><p align = center><a href="guestbook.php3?action=view" onmouseover="window.status=view your comment now;return true" onmouseout="window.status=;return true" title="view your comment now">观看留言</a><br><br>
<?
}
/* if theres no action given, then we must show the main page */
else {
/* get the number of entries written into the guestbook*/
$query = "select name from guestbook";
$result = mysql_query($query);
$number = mysql_numrows($result);
if ($number == "") {
$entry = "还没有人留过言"; }
elseif ($number == "1") {
$entry = "目前留言人数1人"; }
else {
$entry = "目前留言人数 $number 人"; }
echo "<center><br>";
echo "<p>$entry<br>";
echo "<h4><font face="verdana" size="3"><a href="guestbook.php3?action=add" onmouseover="window.status=请您留言;return true" onmouseout="window.status=;return true" title="add your name to our guestbook">请您留言</a></font></h4>";
if ($number > "") {
echo "<h4><font face="verdana" size="3"><a href="guestbook.php3?action=view" onmouseover="window.status=观看留言;return true" onmouseout="window.status=;return true" title="view the names in our guestbook">观看留言</a></font></h4>"; }
echo "</p></center>";
}
?>
<br><small><center>版权所有:<a href="http://personal.668.cc/haitang/index.htm" onmouseover="window.status=pert;return true" onmouseout="window.status=;return true" title="pert">无边天际</a></center></small>
</font>
</body>
</html>