PHP新手上路(十一)

数据库链接

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的链接参数,包括主机名、用户名和密码作为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):

 1   
 2/* $host : your MySQL-host, usually 'localhost' */   
 3/* $user : your MYSQL-username */   
 4/* $password : your MySQL-password */   
 5/* $database : your MySQL-database */   
 6/* $table : your MySQL-table */   
 7/* $page_title : the title of your guestbook-pages */   
 8/* $admin_mail : email-address of the administrator to send the new entries to */   
 9/* $admin_name : the name of the administrator */   
10/* $html_mail : say yes if your mail-agent can handle HTML-mail, else say no */   
11  
12$host = "localhost";   
13$user = "";   
14$password = "";   
15$database = "mydb";   
16$table = "guestbook";   
17$page_title = "pert guestbook";   
18$admin_mail = "[email protected]";   
19$admin_name = "Webmaster";   
20$html_mail = "no";   
21  
  1<html>
  2<head>
  3<title>```
  4 echo $page_title; 
  5```</title>
  6</head>
  7<body bgcolor="#FFFFFF" link="#000000">
  8<font face="Verdana" size="-2">
  9<?   
 10  
 11/* connect to the database */   
 12mysql_pconnect("$host","$user","$password") or die("Can't connect to the SQL-server");   
 13mysql_select_db("$database");   
 14  
 15/* action=view : retrieve data from the database and show it to the user */   
 16if($action == "view") {   
 17  
 18/* function for showing the data */   
 19function search_it($name) {   
 20  
 21/* some vars */   
 22global $offset,$total,$lpp,$dir;   
 23global $table,$html_mail,$admin_name,$admin_mail;   
 24  
 25/* select the data to get out of the database */   
 26$query = "SELECT name, email, job, comments FROM $table";   
 27$result = mysql_query($query);   
 28$total= mysql_numrows($result);   
 29  
 30print "<CENTER><font face="Verdana" size="-2"><a href="guestbook.php3?action=add" onmouseout="window.status='';return true" onmouseover="window.status='Add your name';return true" title="Add your name">加入留言</a></font><br/><br/>";   
 31  
 32if ($total== 0) {   
 33print "<center>此刻没人留言</center><br/><br/>"; }   
 34  
 35elseif ($total&gt; 0) {   
 36  
 37/* default */   
 38$counter=0;   
 39if ($dir=="") $dir="Next";   
 40$lpp=5;   
 41if ($offset==0) $offset=0;   
 42  
 43if ($dir=="Next") {   
 44  
 45if ($total &gt; $lpp) {   
 46  
 47$counter=$offset;   
 48$offset+=$lpp;   
 49$num=$offset;   
 50  
 51if ($num &gt; $total) {   
 52$num=$total; } }   
 53  
 54else {   
 55$num=$total; } }   
 56  
 57elseif ($dir=="Previous") {   
 58  
 59if ($total &gt; $lpp) {   
 60$offset-=$lpp;   
 61  
 62if ($offset &lt; 0) {   
 63$offset=0; }   
 64  
 65$counter=$offset-$lpp;   
 66  
 67if ($counter &lt; 0)   
 68$counter=0;   
 69$num=$counter+$lpp; }   
 70  
 71else {   
 72$num=$total; } }   
 73  
 74while ($counter &lt; $num) {   
 75$j=0;   
 76$j=$counter + 1;   
 77  
 78/* now really grab the data */   
 79$i1=mysql_result($result,$counter,"name");   
 80$i2=mysql_result($result,$counter,"email");   
 81$i3=mysql_result($result,$counter,"job");   
 82$i4=mysql_result($result,$counter,"comments");   
 83  
 84$i4 = stripslashes ("$i4");   
 85  
 86/* print it in a nice layout */   
 87print "<center>n";   
 88print "<table align="CENTER" border="0" valign="TOP" width="400"><tr><td><font face="Verdana" size="-2">n";   
 89print "<hr/>n";   
 90print "<br/><b>Name:</b> $i1n";   
 91print "<br/><b>email:</b><a href="mailto:$i2" onmouseout="window.status='';return true" onmouseover="window.status='Email $i2';return true" title="Email $i2">$i2</a>n";   
 92print "<br/><b>Job:</b> $i3n";   
 93print "<br/><b>Comment:</b>n";   
 94print "<br/>$i4n";   
 95print "</font></td></tr></table>n";   
 96print "</center>n";   
 97$counter++;   
 98}   
 99}   
100mysql_close();   
101}   
102  
103/* execute the function */   
104search_it($name);   
105  
106/* See if we need to put on the NEXT or PREVIOUS buttons */   
107if ($total &gt; $lpp) {   
108echo("<form action="$PHP_SCRIPT" method="POST">n");   
109  
110/* See if we need a PREVIOUS button */   
111if ($offset &gt; $lpp) {   
112echo("<input name="dir" type="submit" value="Previous"/>n"); }   
113  
114/* See if we need a NEXT button */   
115if ($offset &lt; $total) {   
116echo("<input name="dir" type="submit" value="Next"/>n"); }   
117  
118echo("<input name="offset" type="hidden" value="$offset"/>n");   
119echo("<input name="name" type="hidden" value="$name"/>n");   
120echo("</form>");   
121}   
122}   
123  
124/* action=add : show a form where the user can enter data to add to the database */   
125elseif($action == "add") { ?&gt;   
126  
127<table align="CENTER" valign="TOP" width="460">
128<th colspan="2"><p>请您填写留言</p></th>
129<form action="guestbook.php3?action=send" method="POST" name="guestbook">
130<tr>
131<td align="RIGHT" valign="TOP">   
132您的大名:</td>
133<td><input name="name" type="text"/></td>
134</tr>
135<tr>
136<td align="RIGHT" valign="TOP">   
137您的E-mail:</td>
138<td>
139<input name="email" type="text"/></td>
140</tr>
141<tr>
142<td align="RIGHT" valign="TOP">   
143您的工作:</td>
144<td>
145<input name="job" type="text"/></td>
146</tr>
147<tr>
148<td align="RIGHT" valign="TOP">   
149您的留言:</td>
150<td>
151<textarea cols="40" name="comments" rows="6"></textarea>
152<p>
153<input type="submit" value="Submit"/> <input type="Reset" value="Reset"/>
154<a align="RIGHT" href="guestbook.php3?action=view" onmouseout="window.status='';return true" onmouseover="window.status='Read all comments first';return true" title="Read all comments first"><font size="-2">先观看所有的留言</font></a>
155</p></td>
156</tr>
157</form>
158</table>
159
160<?   
161}   
162  
163/* action=send : add the data from the user into the database */   
164elseif($action == "send") {   
165  
166/* check if a HTML-mail should be send or a plain/text mail */   
167if($html_mail == "yes") {   
168mail("$admin_name <$admin_mail>","PHP3 Guestbook Addition","<html><body><font face="Century Gothic"><table border="0" cellspacing="4" width="100%"><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></font></body></html></font></body></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);

?>

1<br/>
 1<p align="CENTER">感谢, ```
 2 echo $name; 
 3```, 您的留言.   
 4<br/><p align="CENTER"><a href="guestbook.php3?action=view" onmouseout="window.status='';return true" onmouseover="window.status='View your comment now';return true" title="View your comment now">观看留言</a><br/><br/>
 5<?   
 6  
 7}   
 8  
 9/* if there's no action given, then we must show the main page */   
10else {   
11  
12/* get the number of entries written into the guestbook*/   
13$query = "SELECT name from guestbook";   
14$result = MYSQL_QUERY($query);   
15$number = MYSQL_NUMROWS($result);   
16  
17if ($number == "") {   
18$entry = "还没有人留过言"; }   
19  
20elseif ($number == "1") {   
21$entry = "目前留言人数1人"; }   
22  
23else {   
24$entry = "目前留言人数 $number 人"; }   
25  
26echo "<CENTER><br/>";   
27echo "<p>$entry<br/>";   
28echo "<h4><font face="Verdana" size="3"><a href="guestbook.php3?action=add" onmouseout="window.status='';return true" onmouseover="window.status='请您留言';return true" title="Add your name to our guestbook">请您留言</a></font></h4>";   
29  
30if ($number &gt; "") {   
31echo "<h4><font face="Verdana" size="3"><a href="guestbook.php3?action=view" onmouseout="window.status='';return true" onmouseover="window.status='观看留言';return true" title="View the names in our guestbook">观看留言</a></font></h4>"; }   
32echo "</p>";   
33}   
34?&gt;   
35<br/><small><center>版权所有:<a href="http://personal.668.cc/haitang/index.htm" onmouseout="window.status='';return true" onmouseover="window.status='pert';return true" title="pert">无边天际</a></center></small>
36
37
38</p></p>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus