澳门新葡萄京官网首页PHP新手上路(11)

数据库链接

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,passwordState of Qatar;
我们将mysql的链接参数,包涵主机名、客户名和密码作为mysql_connect(State of Qatar的参数,同期取得再次来到值为$db,那样,在底下的讲话中,大家就足以将变量$db作为一个一而再连续mysql数据库的连接号来采纳。

mysql_select_db(mydb,$db卡塔尔(قطر‎;
将PHP程序链接到mydb数据库中,那样程序与数据库的链接就完成了。

10.1 二个简约的数据库留言簿

在做到数据库的链接之后,大家就能够对数据库举行一多种的操作。上面是一个总结的数据库留言簿程序:

自身假诺你电话上的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 留言簿程序:

?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;?HTMLHEADTITLE?php echo $page_title; ?/TITLE/HEADBODY BGCOLOR=#FFFFFF LINK=#000000FONT 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 CENTERFONT FACE=Verdana SIZE=-2A HREF=guestbook.php3?action=add onMouseOver=window.status=Add your name;return true onMouseOut=window.status=;return true TITLE=Add your name加入留言/A/FONT/CENTERbrbr;if ($total== 0) {print CENTER此刻没人留言/CENTERbrbr; }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 CENTERn;print TABLE WIDTH=400 BORDER=0 ALIGN=CENTER VALIGN=TOPTRTDFONT FACE=Verdana SIZE=-2n;print HRn;print BRBName:/B $i1n;print BRBemail:/BA HREF=mailto:$i2 onMouseOver=window.status=Email $i2;return true onMouseOut=window.status=;return true TITLE=Email $i2$i2/An;print BRBJob:/B $i3n;print BRBComment:/Bn;print BR$i4n;print /FONT/TD/TR/TABLEn;print /CENTERn;$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=POSTn);/* See if we need a PREVIOUS button */if ($offset  $lpp) {echo(input type=submit value=Previous name=dirn); }/* See if we need a NEXT button */if ($offset  $total) {echo(input type=submit value=Next name=dirn); }echo(input type=hidden name=offset value=$offsetn);echo(input type=hidden name=name value=$namen);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=TOPTH COLSPAN=2P请您填写留言/THFORM NAME=guestbook ACTION=guestbook.php3?action=send METHOD=POSTTRTD ALIGN=RIGHT VALIGN=TOP您的大名:/TDTDINPUT TYPE=text NAME=name/TD/TRTRTD ALIGN=RIGHT VALIGN=TOP您的E-mail:/TDTDINPUT TYPE=text NAME=email/TD/TRTRTD ALIGN=RIGHT VALIGN=TOP您的工作:/TDTDINPUT TYPE=text NAME=job/TD/TRTRTD ALIGN=RIGHT VALIGN=TOP您的留言:/TDTDTEXTAREA NAME=comments COLS=40 ROWS=6/TEXTAREAPINPUT TYPE=submit VALUE=Submit INPUT TYPE=Reset VALUE=ResetA 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 firstFONT 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,HTMLBODYFONT FACE=Century GothicTABLE BORDER=0 WIDTH=100% CELLSPACING=4TR$name ($email) schreef het volgende bericht in het gastenboek :/TRTRTD ALIGN=LEFT /TDTD ALIGN=LEFT NOWRAP /TD/TRTRTD ALIGN=LEFT$comments/TDTD ALIGN=LEFT NOWRAP /TD/TRTRTD ALIGN=LEFT /TDTD ALIGN=LEFT NOWRAP /TD/TRTRTD ALIGN=LEFT您的留言:/TDTD ALIGN=LEFT NOWRAP$name/TD/TRTRTD ALIGN=LEFT您的大名:/TDTD ALIGN=LEFT NOWRAP$email/TD/TRTRTD ALIGN=LEFT您的email:/TDTD ALIGN=LEFT NOWRAP$job/TD/TRTRTD ALIGN=LEFT您的工作:/TD/TR/TABLE/BODY/FONT/HTML, From: $name $emailnReply-To: $name $emailnContent-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);?BRP ALIGN = CENTER感谢, ?php echo $name; ?, 您的留言.BRP ALIGN = CENTERA HREF=guestbook.php3?action=view onMouseOver=window.status=View your comment now;return true onMouseOut=window.status=;return true TITLE=View your comment now观看留言/ABRBR?}/* 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 CENTERBR;echo P$entryBR;echo H4FONT FACE=Verdana SIZE=3A 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 H4FONT FACE=Verdana SIZE=3A 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;}?BRSMALLCENTER版权所有:A HREF= onMouseOver=window.status=pert;return true onMouseOut=window.status=;return true TITLE=pert无边天际/A/CENTER/SMALL/FONT/BODY/HTML

发表评论

电子邮件地址不会被公开。 必填项已用*标注