当前位置:首页 > 专题 > 新手初学PHP > PHP新手上路(十一) 数据库链接

PHP新手上路(十一) 数据库链接

发布:smiling 来源: php粉丝网  添加日期:2013-11-17 浏览: 评论:0 

数据库链接

 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值。让我们来看看下面几句语句:

  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

创建该数据表的命令如下所示:

  1. 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.  
  12. $host = "localhost";  
  13. $user = "";  
  14. $password = "";  
  15. $database = "mydb";  
  16. $table = "guestbook";  
  17. $page_title = "pert guestbook";  
  18. $admin_mail = "pert@21cn.com";  
  19. $admin_name = "Webmaster";  
  20. $html_mail = "no";  
  21.  
  22. ?>  
  23. <HTML>  
  24. <HEAD>  
  25. <TITLE><?php echo $page_title; ?></TITLE>  
  26. </HEAD>  
  27. <BODY BGCOLOR="#FFFFFF" LINK="#000000">  
  28. <FONT FACE="Verdana" SIZE="-2">  
  29. <?  
  30.  
  31. /* connect to the database */ 
  32. mysql_pconnect("$host","$user","$password") or die("Can't connect to the SQL-server");  
  33. mysql_select_db("$database");  
  34.  
  35. /* action=view : retrieve data from the database and show it to the user */ 
  36. if($action == "view") {  
  37.  
  38. /* function for showing the data */ 
  39. function search_it($name) {  
  40.  
  41. /* some vars */ 
  42. global $offset,$total,$lpp,$dir;  
  43. global $table,$html_mail,$admin_name,$admin_mail;  
  44.  
  45. /* select the data to get out of the database */ 
  46. $query = "SELECT name, email, job, comments FROM $table";  
  47. $result = mysql_query($query);  
  48. $total= mysql_numrows($result);  
  49.  
  50. 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>";  
  51.  
  52. if ($total== 0) {  
  53. print "<CENTER>此刻没人留言</CENTER><br><br>"; }  
  54.  
  55. elseif ($total> 0) {  
  56.  
  57. /* default */ 
  58. $counter=0;  
  59. if ($dir=="") $dir="Next";  
  60. $lpp=5;  
  61. if ($offset==0) $offset=0;  
  62.  
  63. if ($dir=="Next") {  
  64.  
  65. if ($total > $lpp) {  
  66.  
  67. $counter=$offset;  
  68. $offset =$lpp;  
  69. $num=$offset;  
  70.  
  71. if ($num > $total) {  
  72. $num=$total; } }  
  73.  
  74. else {  
  75. $num=$total; } }  
  76.  
  77. elseif ($dir=="Previous") {  
  78.  
  79. if ($total > $lpp) {  
  80. $offset-=$lpp;  
  81.  
  82. if ($offset < 0) {  
  83. $offset=0; }  
  84.  
  85. $counter=$offset-$lpp;  
  86.  
  87. if ($counter < 0)  
  88. $counter=0;  
  89. $num=$counter $lpp; }  
  90.  
  91. else {  
  92. $num=$total; } }  
  93.  
  94. while ($counter < $num) {  
  95. $j=0;  
  96. $j=$counter 1;  
  97.  
  98. /* now really grab the data */ 
  99. $i1=mysql_result($result,$counter,"name");  
  100. $i2=mysql_result($result,$counter,"email");  
  101. $i3=mysql_result($result,$counter,"job");  
  102. $i4=mysql_result($result,$counter,"comments");  
  103.  
  104. $i4 = stripslashes ("$i4");  
  105.  
  106. /* print it in a nice layout */ 
  107. print "<CENTER>n";  
  108. print "<TABLE WIDTH=400 BORDER=0 ALIGN=CENTER VALIGN=TOP><TR><TD><FONT FACE="Verdana" SIZE="-2">n";  
  109. print "<HR>n";  
  110. print "<BR><B>Name:</B> $i1n";  
  111. 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";  
  112. print "<BR><B>Job:</B> $i3n";  
  113. print "<BR><B>Comment:</B>n";  
  114. print "<BR>$i4n";  
  115. print "</FONT></TD></TR></TABLE>n";  
  116. print "</CENTER>n";  
  117. $counter ;  
  118. }  
  119. }  
  120. mysql_close();  
  121. }  
  122.  
  123. /* execute the function */ 
  124. search_it($name);  
  125.  
  126. /* See if we need to put on the NEXT or PREVIOUS buttons */ 
  127. if ($total > $lpp) {  
  128. echo("<form action="$PHP_SCRIPT" method="POST">n");  
  129.  
  130. /* See if we need a PREVIOUS button */ 
  131. if ($offset > $lpp) {  
  132. echo("<input type="submit" value="Previous" name=dir>n"); }  
  133.  
  134. /* See if we need a NEXT button */ 
  135. if ($offset < $total) {  
  136. echo("<input type="submit" value="Next" name=dir>n"); }  
  137.  
  138. echo("<input type=hidden name="offset" value="$offset">n");  
  139. echo("<input type=hidden name="name" value="$name">n");  
  140. echo("</form>");  
  141. }  
  142. }  
  143.  
  144. /* action=add : show a form where the user can enter data to add to the database */ 
  145. elseif($action == "add") { ?>  
  146.  
  147. <TABLE WIDTH="460" ALIGN="CENTER" VALIGN="TOP">  
  148. <TH COLSPAN="2"><P>请您填写留言</TH>  
  149. <FORM NAME="guestbook" ACTION="guestbook.php3?action=send" METHOD="POST">  
  150. <TR>  
  151. <TD ALIGN="RIGHT" VALIGN="TOP">  
  152. 您的大名:</TD>  
  153. <TD><INPUT TYPE=text NAME=name></TD>  
  154. </TR>  
  155. <TR>  
  156. <TD ALIGN="RIGHT" VALIGN="TOP">  
  157. 您的E-mail:</TD>  
  158. <TD>  
  159. <INPUT TYPE=text NAME=email></TD>  
  160. </TR>  
  161. <TR>  
  162. <TD ALIGN="RIGHT" VALIGN="TOP">  
  163. 您的工作:</TD>  
  164. <TD>  
  165. <INPUT TYPE=text NAME=job></TD>  
  166. </TR>  
  167. <TR>  
  168. <TD ALIGN="RIGHT" VALIGN="TOP">  
  169. 您的留言:</TD>  
  170. <TD>  
  171. <TEXTAREA NAME=comments COLS=40 ROWS=6></TEXTAREA>  
  172. <P>  
  173. <INPUT TYPE=submit VALUE=Submit> <INPUT TYPE=Reset VALUE=Reset>  
  174. <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>  
  175. </TD>  
  176. </TR>  
  177. </FORM>  
  178. </TABLE>  
  179. </CENTER>  
  180.  
  181. <?  
  182. }  
  183.  
  184. /* action=send : add the data from the user into the database */ 
  185. elseif($action == "send") {  
  186.  
  187. /* check if a HTML-mail should be send or a plain/text mail */ 
  188. if($html_mail == "yes") {  
  189. 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());  
  190. }  
  191. /* MySQL really hates it when you try to put things with ' or " characters into a database, so strip these...*/ 
  192. $comments = addslashes ("$comments");  
  193. $query = "INSERT INTO guestbook VALUES('','$name', '$email', '$job', '$comments')";  
  194. $result = MYSQL_QUERY($query);  
  195.  
  196. ?>  
  197. <BR><P ALIGN = CENTER>感谢, <?php echo $name; ?>, 您的留言.  
  198. <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>  
  199. <?  
  200.  
  201. }  
  202. /* if there's no action given, then we must show the main page */ 
  203. else {  
  204.  
  205. /* get the number of entries written into the guestbook*/ 
  206. $query = "SELECT name from guestbook";  
  207. $result = MYSQL_QUERY($query);  
  208. $number = MYSQL_NUMROWS($result);  
  209.  
  210. if ($number == "") {  
  211. $entry = "还没有人留过言"; }  
  212.  
  213. elseif ($number == "1") {  
  214. $entry = "目前留言人数1人"; }  
  215.  
  216. else {  
  217. $entry = "目前留言人数 $number 人"; }  
  218.  
  219. echo "<CENTER><BR>";  
  220. echo "<P>$entry<BR>";  
  221. 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>";  
  222.  
  223. if ($number > "") {  
  224. 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>"; }  
  225. echo "</P></CENTER>";  
  226. }  
  227. ?>  
  228. <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>  
  229. </FONT>  
  230. </BODY>  
  231. </HTML> 

Tags:php新手入门 php新手上路

分享到: 收藏

相关文章