Basic operation methods of PHP on text database_PHP tutorial

WBOY
Release: 2016-07-13 17:33:06
Original
972 people have browsed it

php(做为现在的主流开发语言)实现对文本数据库的数据显示、加入、修改、删除、查询五大基本操作的方法
我用一个留言本程序作为例子,阐述php(做为现在的主流开发语言)实现对文本数据库的数据显示、加入、修改、删除、查询五大基本操作的方法。
此文本数据库共有字段10个:客户IP、发言时间、客户名、客户EMAIL、客户主页地址、留言表情图片名、客户QQ、客户形象图片、留言内容、管理员回复内容。

1、加入数据程序段。

$date=date("Y-m-d H:i:s");//取得系统时间
$ip = $HTTP_SERVER_VARS[REMOTE_ADDR]; //取得发言的IP地址
$text=encode($gb_text);//去掉留言内容后面的空格.
$fp=fopen("gb.dat","a");//以只写模式打开gb.dat文本文件,文件指针指向文件尾部.
$str=$ip." ".$date." ".$gb_name." ".$gb_email." ".$gb_home." ".$face." ".$gb_qq." ".$head." ".$text." ".$reply." ";//将所有留言的数据赋予变量$str," "的目的是用来今后作数据分割时的数据间隔符号。
fwrite($fp,$str);//将数据写入文件
fclose($fp);//关闭文件
showmessage("留言成功!","index.php(做为现在的主流开发语言)","3");//留言成功,3秒后自动返回主界面。
其中的$gb_name 、$gb_email、$gb_home、$face、$gb_qq、$head、$gb_text、$reply是由发言表单传过来的数据。

2、数据显示程序段

<?
if (file_exists("gb.dat")){//Check whether the file exists
$array=file("gb.dat");//Read the entire content of the file into Array $array
$arr=array_reverse($array);//Reverse the data in $array (that is, the last row is the first row, and so on) and read into each unit of array $arr ($ arr[0]...).
$num=count($array);//Get the number of information in the array $array (one line per line)
if ($num>0){//If the number of information is greater than zero (i.e. text database Not empty)
$total=ceil($num/$pagesize);//Calculate the total number of pages (take the largest integer, that is, round up any decimal point, $pagesize is the default value displayed on each page Number of information)
if($page<1){//If the current page number is less than 1
$page=1;//Assign the value to 1
}
$number=($page -1)*$pagesize;//Calculate the digital number of the first message currently displayed (the digital number starts from zero, mainly to achieve the purpose of corresponding to the array unit number)
for($i=0;$i <=$pagesize-1;$i++){//Enter the loop
$row=explode(" ",$arr[$number]);//Use " " as the separator to divide each number in the array $arr $number units of data, and assign these data to the array $rom
list($ip,$datetime,$name,$email,$home,$face,$qq,$head,$text,$reply) =$row;//Assign the unit data in the array $row to the variables in the brackets in order
?>
<img src=<? echo $head ?>>//Display the customer image picture
<br>
<font color="#0099CC">Nickname【<? echo $name ?><font size="2">】<br>//Display customer name
Published on: <? echo $datetime ?>//Display the message publishing time
<br>
<img src=<? echo $face ?>>//Display the customer message emoticon picture
<? echo $name ?> said: <? echo $text; ?>//Display customer message content
<br>
<? echo $reply ?>//Display reply content
<br>
<a href="< ? echo $home ?>" target="_blank">Visit the homepage of <? echo $name ?></a>//Hyperlink to the customer homepage
<a href="mailto:<? echo $email ? >">Send an email to <? echo $name ?></a>//The connection of the customer’s E-MAIL
<? echo $name ?>’s QQ number is <? echo $qq ?>//Display the customer The IP address of the QQ number
<? echo $name ?> is <? echo $ip ?>" //Display the customer’s IP address
<a href="reply.php(as now mainstream development language)?time=<? echo $datetime ?>">Reply</a>//Connection statement for message reply
<a href="del.php(as now mainstream development language) ?time=<? echo $datetime ?>">Delete</a>//The statement for message deletion (use the customer message time $datetime as the deletion identifier) ​​
<br>
<?
if ($number == $num-1)//If the unit number of the array is equal to the total number of messages minus one (because the unit number starts with zero, this means this is the last message)
{
break;//Jump out of the loop
}
$number = $number + 1; //Add 1 to the array unit number
}//End of loop
}
if ($page <> 1)//If the current page number is not equal to 1
{
$back = $page - 1;//Decrease the current page number by 1, and assign this value to the variable $back
echo "<a href=index.php(as the current mainstream development language)?page=1>First page</a>";//Display the link to the first page
echo "<a href=index.php(as the current mainstream development language)?page=$back>Previous page</a>"; The current page number is equal to $back, and Display its connection
}
if ($page <> $total)//If the current page number is not equal to the total page number (the last page number)
{
$next = $ page + 1;//Add 1 to the current page number and assign it to the variable $next
echo " <a href=index.php(as the current mainstream development language)?page=$next> Next page</a>";//Show the next page link
echo "<a href=index.php(as the current mainstream development language)?page=$total>Finally One page</a>"; Display the last page link
}
echo "Number of pages: $page / $total";//Display the current page number and display the last page number
echo "There are $num messages in total";//Display message number information
}
else {
echo "

There are currently no messages! </center>";//The information displayed if the file content is empty
}
else {
echo "<center>The data file is lost, please contact the administrator! Or leave a message to re-create the data file! </center>";//If the file does not exist, the information displayed
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508651.htmlTechArticlephp (as the current mainstream development language) realizes data display, addition, modification, deletion of text database, To query the five basic operations, I use a guestbook program as an example...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template