Discuz教程网

PHP新手上路 - 011 - 数据库链接

[复制链接]
authicon php1314 发表于 2010-12-5 10:45:11 | 显示全部楼层 |阅读模式
数据库链接
10. PHP最大的特色就是操作数据库的能力特别的强大,PHP提供对多种数据库的支持。
  通过PHP你可以轻松的连接到数据库,请求数据并将其显示在你的web站点中,甚至修改数据库中的数据。在这一节里我们主要以在互联网上跟PHP一起使用得最多的MySQL数据库为例,介绍一下相关的MySQL数据库的操作函数以及数据库的基本操作等方面的知识。
在MySQL数据库中,我们用来连接数据库的函数有两个,它们分别为:

  1. integer mysql_connect(string host,string user,string password);
  2. integer mysql_pconnect(string host,string user,string password);
复制代码

mysql_connect函数和mysql_pconnect函数都是对指定主机上MySQL数据库的连接,如果该数据库位于一个不同的端口,则可以在主机名后加上冒号和端口号。函数的参数也可以缺省不填,如果不填参数,默认的主机名是“localhost”,用户名为数据库管理员,默认值为“root”,密码为空。与数据库连接成功之后,这两个函数都可以返回一个连接号,如果连接失败,则返回一个false值。让我们来看看下面几句语句:

  1. <?
  2. $db=mysql_connect("localhost","user","password");
  3. mysql_select_db("mydb",$db);
  4. ?>
复制代码

注释:
$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. <?php
  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. $host = "localhost";
  12. $user = "";
  13. $password = "";
  14. $database = "mydb";
  15. $table = "guestbook";
  16. $page_title = "pert guestbook";
  17. $admin_mail = "pert@21cn.com";
  18. $admin_name = "Webmaster";
  19. $html_mail = "no";
  20. ?>
  21. <HTML>
  22. <HEAD>
  23. <TITLE><?php echo $page_title; ?></TITLE>
  24. </HEAD>
  25. <BODY BGCOLOR="#FFFFFF" LINK="#000000">
  26. <FONT FACE="Verdana" SIZE="-2">
  27. <?
  28. /* connect to the database */
  29. mysql_pconnect("$host","$user","$password") or die("Can't connect to the SQL-server");
  30. mysql_select_db("$database");
  31. /* action=view : retrieve data from the database and show it to the user */
  32. if($action == "view") {
  33. /* function for showing the data */
  34. function search_it($name) {
  35. /* some vars */
  36. global $offset,$total,$lpp,$dir;
  37. global $table,$html_mail,$admin_name,$admin_mail;
  38. /* select the data to get out of the database */
  39. $query = "SELECT name, email, job, comments FROM $table";
  40. $result = mysql_query($query);
  41. $total= mysql_numrows($result);
  42. 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>";
  43. if ($total== 0) {
  44. print "<CENTER>此刻没人留言</CENTER><br><br>"; }
  45. elseif ($total> 0) {
  46. /* default */
  47. $counter=0;
  48. if ($dir=="") $dir="Next";
  49. $lpp=5;
  50. if ($offset==0) $offset=0;
  51. if ($dir=="Next") {
  52. if ($total > $lpp) {
  53. $counter=$offset;
  54. $offset =$lpp;
  55. $num=$offset;
  56. if ($num > $total) {
  57. $num=$total; } }
  58. else {
  59. $num=$total; } }
  60. elseif ($dir=="Previous") {
  61. if ($total > $lpp) {
  62. $offset-=$lpp;
  63. if ($offset < 0) {
  64. $offset=0; }
  65. $counter=$offset-$lpp;
  66. if ($counter < 0)
  67. $counter=0;
  68. $num=$counter $lpp; }
  69. else {
  70. $num=$total; } }
  71. while ($counter < $num) {
  72. $j=0;
  73. $j=$counter 1;
  74. /* now really grab the data */
  75. $i1=mysql_result($result,$counter,"name");
  76. $i2=mysql_result($result,$counter,"email");
  77. $i3=mysql_result($result,$counter,"job");
  78. $i4=mysql_result($result,$counter,"comments");
  79. $i4 = stripslashes ("$i4");
  80. /* print it in a nice layout */
  81. print "<CENTER>n";
  82. print "<TABLE WIDTH=400 BORDER=0 ALIGN=CENTER VALIGN=TOP><TR><TD><FONT FACE="Verdana" SIZE="-2">n";
  83. print "<HR>n";
  84. print "<BR><B>Name:</B> $i1n";
  85. 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";
  86. print "<BR><B>Job:</B> $i3n";
  87. print "<BR><B>Comment:</B>n";
  88. print "<BR>$i4n";
  89. print "</FONT></TD></TR></TABLE>n";
  90. print "</CENTER>n";
  91. $counter ;
  92. }
  93. }
  94. mysql_close();
  95. }
  96. /* execute the function */
  97. search_it($name);
  98. /* See if we need to put on the NEXT or PREVIOUS buttons */
  99. if ($total > $lpp) {
  100. echo("<form action="$PHP_SCRIPT" method="POST">n");
  101. /* See if we need a PREVIOUS button */
  102. if ($offset > $lpp) {
  103. echo("<input type="submit" value="Previous" name=dir>n"); }
  104. /* See if we need a NEXT button */
  105. if ($offset < $total) {
  106. echo("<input type="submit" value="Next" name=dir>n"); }
  107. echo("<input type=hidden name="offset" value="$offset">n");
  108. echo("<input type=hidden name="name" value="$name">n");
  109. echo("</form>");
  110. }
  111. }
  112. /* action=add : show a form where the user can enter data to add to the database */
  113. elseif($action == "add") { ?>
  114. <TABLE WIDTH="460" ALIGN="CENTER" VALIGN="TOP">
  115. <TH COLSPAN="2"><P>请您填写留言</TH>
  116. <FORM NAME="guestbook" ACTION="guestbook.php3?action=send" METHOD="POST">
  117. <TR>
  118. <TD ALIGN="RIGHT" VALIGN="TOP">
  119. 您的大名:</TD>
  120. <TD><INPUT TYPE=text NAME=name></TD>
  121. </TR>
  122. <TR>
  123. <TD ALIGN="RIGHT" VALIGN="TOP">
  124. 您的E-mail:</TD>
  125. <TD>
  126. <INPUT TYPE=text NAME=email></TD>
  127. </TR>
  128. <TR>
  129. <TD ALIGN="RIGHT" VALIGN="TOP">
  130. 您的工作:</TD>
  131. <TD>
  132. <INPUT TYPE=text NAME=job></TD>
  133. </TR>
  134. <TR>
  135. <TD ALIGN="RIGHT" VALIGN="TOP">
  136. 您的留言:</TD>
  137. <TD>
  138. <TEXTAREA NAME=comments COLS=40 ROWS=6></TEXTAREA>
  139. <P>
  140. <INPUT TYPE=submit VALUE=Submit> <INPUT TYPE=Reset VALUE=Reset>
  141. <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>
  142. </TD>
  143. </TR>
  144. </FORM>
  145. </TABLE>
  146. </CENTER>
  147. <?
  148. }
  149. /* action=send : add the data from the user into the database */
  150. elseif($action == "send") {
  151. /* check if a HTML-mail should be send or a plain/text mail */
  152. if($html_mail == "yes") {
  153. 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());
  154. }

  155. /* MySQL really hates it when you try to put things with ' or " characters into a database, so strip these...*/
  156. $comments = addslashes ("$comments");
  157. $query = "INSERT INTO guestbook VALUES('','$name', '$email', '$job', '$comments')";
  158. $result = MYSQL_QUERY($query);
  159. ?>
  160. <BR><P ALIGN = CENTER>感谢, <?php echo $name; ?>, 您的留言.
  161. <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>
  162. <?

  163. }
  164. /* if there's no action given, then we must show the main page */
  165. else {
  166. /* get the number of entries written into the guestbook*/
  167. $query = "SELECT name from guestbook";
  168. $result = MYSQL_QUERY($query);
  169. $number = MYSQL_NUMROWS($result);
  170. if ($number == "") {
  171. $entry = "还没有人留过言"; }
  172. elseif ($number == "1") {
  173. $entry = "目前留言人数1人"; }
  174. else {
  175. $entry = "目前留言人数 $number 人"; }
  176. echo "<CENTER><BR>";
  177. echo "<P>$entry<BR>";
  178. 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>";
  179. if ($number > "") {
  180. 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>"; }
  181. echo "</P></CENTER>";
  182. }
  183. ?>
  184. <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>
  185. </FONT>
  186. </BODY>
  187. </HTML>
复制代码







上一篇:PHP新手上路 - 010 - 简易banner动态更替
下一篇:PHP新手上路 - 012 - 使用PHP来操作Oracle数据库
authicon 风中徜徉 发表于 2011-5-14 18:00:01 | 显示全部楼层
支持楼主,顶一下
authicon 纤陌陌 发表于 2011-5-16 07:59:52 | 显示全部楼层
好东西要顶的。
authicon 月之海洋 发表于 2011-5-20 22:59:35 | 显示全部楼层
支持楼主,顶一下
authicon kikiya11 发表于 2011-5-21 20:59:48 | 显示全部楼层
我回不回呢 考虑再三 还是不回了吧 ^_^
authicon Pianissimo 发表于 2011-5-23 09:59:50 | 显示全部楼层
这个要顶起来啊
authicon shakesxia 发表于 2011-5-24 20:59:42 | 显示全部楼层
不错不错,我喜欢
authicon TRACYFLYING 发表于 2011-5-25 05:59:34 | 显示全部楼层
感谢分享  收下了·····
authicon lilac_yao 发表于 2011-5-25 08:59:38 | 显示全部楼层
这个贴不错!!!
authicon TRACYFLYING 发表于 2011-6-26 13:59:49 | 显示全部楼层
看一下啊,嘻嘻
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 21:54

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表