文章导航PC6首页软件下载单机游戏安卓资源苹果资源

pc软件新闻网络操作系统办公工具编程服务器软件评测

安卓新闻资讯应用教程刷机教程安卓游戏攻略tv资讯深度阅读综合安卓评测

苹果ios资讯苹果手机越狱备份教程美化教程ios软件教程mac教程

单机游戏角色扮演即时战略动作射击棋牌游戏体育竞技模拟经营其它游戏游戏工具

网游cf活动dnf活动lol周免英雄lol礼包

手游最新动态手游评测手游活动新游预告手游问答

您的位置:首页网页设计PHP教程 → PHP新手上路(十一)

PHP新手上路(十一)

时间:2004/11/7 3:09:00来源:本站整理作者:蓝点我要评论(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值。让我们来看看下面几句语句:

<?

$db=mysql_connect("localhost","user","password");

mysql_select_db("mydb",$db);

?>

注释:

$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):



<?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";



?>




<?php echo=""></?php>









<?



/* connect to the database */

mysql_pconnect("$host","$user","$password") or die("Can't 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 "加入留言

";



if ($total== 0) {

print "此刻没人留言

"; }



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 "n";

print "

n";

print "
n";

print "
Name:$i1n";

print "
email:$i2n";

print "
Job:$i3n";

print "
Comment:n";

print "
$i4n";

print "

n";

print "n";

$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("n");



/* See if we need a PREVIOUS button */

if ($offset > $lpp) {

echo("n"); }



/* See if we need a NEXT button */

if ($offset < $total) {

echo("n"); }



echo("n");

echo("n");

echo("");

}

}



/* action=add : show a form where the user can enter data to add to the database */

elseif($action == "add") { ?>










































请您填写留言



您的大名:


您的E-mail:





您的工作:





您的留言:







  

先观看所有的留言








<?

}



/* 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","$name ($email) schreef het volgende bericht in het gastenboek :







$comments






您的留言:
$name
您的大名:
$email
您的email:
$job
您的工作:

", "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);



?>

感谢,  , 您的留言.

观看留言



<?



}



/* if there's 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 "


";

echo "

$entry
";

echo "

请您留言

";



if ($number > "") {

echo "

观看留言

"; }

echo "

";

}

?>


版权所有:无边天际





  

相关阅读 Windows错误代码大全 Windows错误代码查询激活windows有什么用Mac QQ和Windows QQ聊天记录怎么合并 Mac QQ和Windows QQ聊天记录Windows 10自动更新怎么关闭 如何关闭Windows 10自动更新windows 10 rs4快速预览版17017下载错误问题Win10秋季创意者更新16291更新了什么 win10 16291更新内容windows10秋季创意者更新时间 windows10秋季创意者更新内容kb3150513补丁更新了什么 Windows 10补丁kb3150513是什么

文章评论
发表评论

热门文章 没有查询到任何记录。

最新文章 PHP入门教程书籍介绍用可牛为MM无痛纹身刺 编译apache的rewrite模块实例Apache中URL Rewrite技术实现方式在PHP中有urldecode()等函数来解决网页URL编PHP中的mb_convert_encoding转换编码与icon

人气排行 在PHP中有urldecode()等函数来解决网页URL编dede 完美分页效果! pagelist修改,实现门PHP入门教程书籍介绍用IdHTTP获取UTF-8编码的网页apache泛域名解析+泛域名指向处理文件ThinkTemplate模板引擎的设计和使用方法如何用php将任何格式视频转为flv破解防盗链图片的php函数