PHP新手上路(十一)
数据库链接 <P></P><P>10. PHP最大的特色就是操作数据库的能力特别的强大,PHP提供对多种数据库的支持。 </P><P> 通过PHP你可以轻松的连接到数据库,请求数据并将其显示在你的web站点中,甚至修改数据库中的数据。在这一节里我们主要以在互联网上跟PHP一起使用得最多的MySQL数据库为例,介绍一下相关的MySQL数据库的操作函数以及数据库的基本操作等方面的知识。 </P><P>在MySQL数据库中,我们用来连接数据库的函数有两个,它们分别为: <BR>integer mysql_connect(string host,string user,string password); <BR>integer mysql_pconnect(string host,string user,string password); <BR>mysql_connect函数和mysql_pconnect函数都是对指定主机上MySQL数据库的连接,如果该数据库位于一个不同的端口,则可以在主机名后加上冒号和端口号。函数的参数也可以缺省不填,如果不填参数,默认的主机名是“localhost”,用户名为数据库管理员,默认值为“root”,密码为空。与数据库连接成功之后,这两个函数都可以返回一个连接号,如果连接失败,则返回一个false值。让我们来看看下面几句语句: <BR><? <BR>$db=mysql_connect("localhost","user","password"); <BR>mysql_select_db("mydb",$db); <BR>?> <BR>注释: <BR>$db=mysql_connect("localhost","user","password"); 我们将mysql的链接参数,包括主机名、用户名和密码作为mysql_connect()的参数,同时得到返回值为$db,这样,在下面的语句中,我们就可以将变量$db作为一个连接mysql数据库的连接号来使用。 <BR>mysql_select_db("mydb",$db); 将PHP程序链接到mydb数据库中,这样程序与数据库的链接就完成了。 </P><P>10.1 一个简易的数据库留言簿 </P><P> 在完成数据库的链接之后,我们就可以对数据库进行一系列的操作。下面是一个简易的数据库留言簿程序(guestbook.php3): </P><P> 我假设你机子上的MySQL数据库以及管理MYSQL数据库的工具 Phpmyadmin_2. 0.5都已经安装完成,并且可以正常工作。 </P><P>我们要做的第一件事情是创建一个留言数据库,假定名字为: mydb。 </P><P>1、启动浏览器,打开Phpmyadmin_2. 0.5 的管理WEB界面。 </P><P>2、在“Create new database”文本框内输入数据库名称mydb,然后按create按键。 </P><P> 下一步,我们要在该留言数据库下创建一个数据表,假定名字为: guestbook。 </P><P>创建该数据表的命令如下所示: </P><P>CREATE TABLE guestbook (ID INT NOT NULL AUTO_INCREMENT, name CHAR(250), email CHAR(250), job CHAR(250), comments BLOB, PRIMARY KEY(ID)); </P><P>最后,将下面的留言簿程序挎贝到你机子的可写目录下面,并保存成guestbook.php3文件。就这么简单,你已经有了自己的留言簿了。 </P><P>10.2 留言簿程序(guestbook.php3): </P><P><?php <BR>/* $host : your MySQL-host, usually 'localhost' */ <BR>/* $user : your MYSQL-username */ <BR>/* $password : your MySQL-password */ <BR>/* $database : your MySQL-database */ <BR>/* $table : your MySQL-table */ <BR>/* $page_title : the title of your guestbook-pages */ <BR>/* $admin_mail : email-address of the administrator to send the new entries to */ <BR>/* $admin_name : the name of the administrator */ <BR>/* $html_mail : say yes if your mail-agent can handle HTML-mail, else say no */ </P><P>$host = "localhost"; <BR>$user = ""; <BR>$password = ""; <BR>$database = "mydb"; <BR>$table = "guestbook"; <BR>$page_title = "pert guestbook"; <BR>$admin_mail = "pert@21cn.com"; <BR>$admin_name = "Webmaster"; <BR>$html_mail = "no"; </P><P>?> <BR><HTML> <BR><HEAD> <BR><TITLE><?php echo $page_title; ?></TITLE> <BR></HEAD> <BR><BODY BGCOLOR="#FFFFFF" LINK="#000000"> <BR><FONT FACE="Verdana" SIZE="-2"> <BR><? </P><P>/* connect to the database */ <BR>mysql_pconnect("$host","$user","$password") or die("Can't connect to the SQL-server"); <BR>mysql_select_db("$database"); </P><P>/* action=view : retrieve data from the database and show it to the user */ <BR>if($action == "view") { </P><P>/* function for showing the data */ <BR>function search_it($name) { </P><P>/* some vars */ <BR>global $offset,$total,$lpp,$dir; <BR>global $table,$html_mail,$admin_name,$admin_mail; </P><P>/* select the data to get out of the database */ <BR>$query = "SELECT name, email, job, comments FROM $table"; <BR>$result = mysql_query($query); <BR>$total= mysql_numrows($result); </P><P>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>"; </P><P>if ($total== 0) { <BR>print "<CENTER>此刻没人留言</CENTER><br><br>"; } </P><P>elseif ($total> 0) { </P><P>/* default */ <BR>$counter=0; <BR>if ($dir=="") $dir="Next"; <BR>$lpp=5; <BR>if ($offset==0) $offset=0; </P><P>if ($dir=="Next") { </P><P>if ($total > $lpp) { </P><P>$counter=$offset; <BR>$offset+=$lpp; <BR>$num=$offset; </P><P>if ($num > $total) { <BR>$num=$total; } } </P><P>else { <BR>$num=$total; } } </P><P>elseif ($dir=="Previous") { </P><P>if ($total > $lpp) { <BR>$offset-=$lpp; </P><P>if ($offset < 0) { <BR>$offset=0; } </P><P>$counter=$offset-$lpp; </P><P>if ($counter < 0) <BR>$counter=0; <BR>$num=$counter+$lpp; } </P><P>else { <BR>$num=$total; } } </P><P>while ($counter < $num) { <BR>$j=0; <BR>$j=$counter + 1; </P><P>/* now really grab the data */ <BR>$i1=mysql_result($result,$counter,"name"); <BR>$i2=mysql_result($result,$counter,"email"); <BR>$i3=mysql_result($result,$counter,"job"); <BR>$i4=mysql_result($result,$counter,"comments"); </P><P>$i4 = stripslashes ("$i4"); </P><P>/* print it in a nice layout */ <BR>print "<CENTER>n"; <BR>print "<TABLE WIDTH=400 BORDER=0 ALIGN=CENTER VALIGN=TOP><TR><TD><FONT FACE="Verdana" SIZE="-2">n"; <BR>print "<HR>n"; <BR>print "<BR><B>Name:</B> $i1n"; <BR>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"; <BR>print "<BR><B>Job:</B> $i3n"; <BR>print "<BR><B>Comment:</B>n"; <BR>print "<BR>$i4n"; <BR>print "</FONT></TD></TR></TABLE>n"; <BR>print "</CENTER>n"; <BR>$counter++; <BR>} <BR>} <BR>mysql_close(); <BR>} </P><P>/* execute the function */ <BR>search_it($name); </P><P>/* See if we need to put on the NEXT or PREVIOUS buttons */ <BR>if ($total > $lpp) { <BR>echo("<form action="$PHP_SCRIPT" method="POST">n"); </P><P>/* See if we need a PREVIOUS button */ <BR>if ($offset > $lpp) { <BR>echo("<input type="submit" value="Previous" name=dir>n"); } </P><P>/* See if we need a NEXT button */ <BR>if ($offset < $total) { <BR>echo("<input type="submit" value="Next" name=dir>n"); } </P><P>echo("<input type=hidden name="offset" value="$offset">n"); <BR>echo("<input type=hidden name="name" value="$name">n"); <BR>echo("</form>"); <BR>} <BR>} </P><P>/* action=add : show a form where the user can enter data to add to the database */ <BR>elseif($action == "add") { ?> </P><P><TABLE WIDTH="460" ALIGN="CENTER" VALIGN="TOP"> <BR><TH COLSPAN="2"><P>请您填写留言</TH> <BR><FORM NAME="guestbook" ACTION="guestbook.php3?action=send" METHOD="POST"> <BR><TR> <BR><TD ALIGN="RIGHT" VALIGN="TOP"> <BR>您的大名:</TD> <BR><TD><INPUT TYPE=text NAME=name></TD> <BR></TR> <BR><TR> <BR><TD ALIGN="RIGHT" VALIGN="TOP"> <BR>您的E-mail:</TD> <BR><TD> <BR><INPUT TYPE=text NAME=email></TD> <BR></TR> <BR><TR> <BR><TD ALIGN="RIGHT" VALIGN="TOP"> <BR>您的工作:</TD> <BR><TD> <BR><INPUT TYPE=text NAME=job></TD> <BR></TR> <BR><TR> <BR><TD ALIGN="RIGHT" VALIGN="TOP"> <BR>您的留言:</TD> <BR><TD> <BR><TEXTAREA NAME=comments COLS=40 ROWS=6></TEXTAREA> <BR><P> <BR><INPUT TYPE=submit VALUE=Submit> <INPUT TYPE=Reset VALUE=Reset> <BR><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> <BR></TD> <BR></TR> <BR></FORM> <BR></TABLE> <BR></CENTER> </P><P><? <BR>} </P><P>/* action=send : add the data from the user into the database */ <BR>elseif($action == "send") { </P><P>/* check if a HTML-mail should be send or a plain/text mail */ <BR>if($html_mail == "yes") { <BR>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()); <BR>} </P><P><BR>/* MySQL really hates it when you try to put things with ' or " characters into a database, so strip these...*/ <BR>$comments = addslashes ("$comments"); <BR>$query = "INSERT INTO guestbook VALUES('','$name', '$email', '$job', '$comments')"; <BR>$result = MYSQL_QUERY($query); </P><P>?> <BR><BR><P ALIGN = CENTER>感谢, <?php echo $name; ?>, 您的留言. <BR><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> <BR><? </P><P>} </P><P>/* if there's no action given, then we must show the main page */ <BR>else { </P><P>/* get the number of entries written into the guestbook*/ <BR>$query = "SELECT name from guestbook"; <BR>$result = MYSQL_QUERY($query); <BR>$number = MYSQL_NUMROWS($result); </P><P>if ($number == "") { <BR>$entry = "还没有人留过言"; } </P><P>elseif ($number == "1") { <BR>$entry = "目前留言人数1人"; } </P><P>else { <BR>$entry = "目前留言人数 $number 人"; } </P><P>echo "<CENTER><BR>"; <BR>echo "<P>$entry<BR>"; <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>"; </P><P>if ($number > "") { <BR>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>"; } <BR>echo "</P></CENTER>"; <BR>} <BR>?> <BR><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> <BR></FONT> <BR></BODY> <BR></HTML> </P>
Pages:
[1]