What are the methods of operating text databases in PHP? Five ways to operate text databases with PHP

不言
Release: 2023-04-03 11:12:02
Original
3523 people have browsed it

What are the operations of php on text database data? PHP implements five basic operations methods for displaying, adding, modifying, deleting, and querying text database data. So, let’s take a look at how PHP implements text database data operation methods.
I use a guestbook program as an example to explain how PHP implements the five basic operations of data display, addition, modification, deletion, and query on text databases.
This text database has a total of 10 fields: customer IP, speaking time, customer name, customer EMAIL, customer homepage address, message emoticon picture name, customer QQ, customer image picture, message content, and administrator reply content.

1. Add data program segments.

$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."\n";//将所有留言的数据赋予变量$str," "的目的是用来今后作数据分割时的数据间隔符号。 
fwrite($fp,$str);//将数据写入文件 
fclose($fp);//关闭文件 
showmessage("留言成功!","index.php","3");//留言成功,3秒后自动返回主界面。
Copy after login

$gb_name, $gb_email, $gb_home, $face, $gb_qq, $head, $gb_text, $reply are the data passed from the speech form.

2. Data display program section

<? 
if (file_exists("gb.dat")){//检测文件是否存在 
$array=file("gb.dat");//将文件全部内容读入到数组$array 
$arr=array_reverse($array);//将$array里的数据安行翻转排列(即最后一行当第一行,依此类推)读入数组$arr的每一个单元($arr[0]...)。 
$num=count($array);//获取数组$array里的信息数(一行为一条信息) 
if ($num>0){//如果信息数大于零(即文本数据库不为空) 
$total=ceil($num/$pagesize);//计算总页数(取最大整数,即凡有小数点都进一取整,$pagesize为预设的每页显示的信息数) 
if($page<1){//如果当前页面数码号小于1 
$page=1;//则赋值为1 
} 
$number=($page-1)*$pagesize;//计算当前所显示第一个留言的数码号(数码号从零开始,主要是达到与数组单元号对应的目的) 
for($i=0;$i<=$pagesize-1;$i++){//进入循环 
$row=explode(" ",$arr[$number]);//以" "作为分割符,分割数组$arr中每第$number个单元的数据,并将这些数据赋予数组$rom 
list($ip,$datetime,$name,$email,$home,$face,$qq,$head,$text,$reply)=$row;//将数组$row里的单元数据按顺序赋予括号里的变量 
?> 
<img src=<? echo $head ?> >//显示客户形象图片 
<br> 
<font color="#0099CC">昵称【<? echo $name ?><font size="2">】<br>//显示客户名 
发表于:<? echo $datetime ?>//显示留言发表时间 
<br> 
<img src=<? echo $face ?>>//显示客户留言表情图片 
<? echo $name ?>说:<? echo $text; ?>//显示客户留言内容 
<br> 
<? echo $reply ?>//显示回复内容 
<br> 
<a href="<? echo $home ?>" target="_blank">访问<? echo $name ?>的主页</a>//客户主页的超连接 
<a href="mailto:<? echo $email ?>">给<? echo $name ?>发信</a>//客户E-MAIL的连接 
<? echo $name ?>的QQ号码是<? echo $qq ?>//显示客户的QQ号码 
<? echo $name ?>的IP地址为<? echo $ip ?>" //显示客户的IP地址 
<a href="reply.php?time=<? echo $datetime ?>">回复</a>//留言回复的连接语句 
<a href="del.php?time=<? echo $datetime ?>">删除</a>//留言删除的语句(以客户留言时间$datetime作为删除标识) 
<br> 
<? 
if ($number == $num-1)//如果数组的单元号等于总留言数减一(因为单元号以零开始的,所以这意味着这是最后一条留言) 
{ 
break;//跳出循环 
} 
$number = $number + 1; //数组单元号加1 
}//循环结束符 
} 
if ($page <> 1)//如果当前页面数码号不等于1 
{ 
$back = $page - 1;//当前页面数码号减1,并将此值赋予变量$back 
echo "<a href=index.php?page=1>第一页</a>";//显示第一页的连接 
echo " <a href=index.php?page=$back>上一页</a>" ;当前页面数码号等于$back,并显示其连接 
} 
if ($page <> $total)//如果当前页面数码号不等于总页数号(最后一页数码号) 
{ 
$next = $page + 1;//当前页面数码号加1并赋予变量$next 
echo " <a href=index.php?page=$next>下一页</a>" ;//显示下一页连接 
echo " <a href=index.php?page=$total>最后一页</a>"; 显示最后一页连接 
} 
echo "页数:$page / $total";//显示当前页面数码号和显示最后一页数码号 
echo "共有 $num 条留言";//显示留言数信息 
} 
else { 
echo "<center>当前没有任何留言!</center>";//如果文件内容为空则显示的信息 
} 
else { 
echo "<center>数据文件丢失,请联系管理员!或发布留言重新建立数据文件!</center>";//如果文件不存在显示的信息 
}
Copy after login

3. Data modification program section

$list=file("gb.dat");//读取整个gb.dat文件到数组$list,数组每一个元素为一条留言($list[0]是第一条留言的数据、$list[1]是第一条留言的数据..... 
$n=count($list);//计算$list内容里的留言总数,并赋予变量$n 
if ($n>0){ //如果留言数大于0
$fp=fopen("gb.dat","w");//则以只写模式打开文件gb.dat 
$gb_reply=encode($gb_reply); 
for ($i=0;$i<$n;$i++) {//进入循环 
if(eregi($ttime,$list[$i])){//将送来发留言时间$ttime与数组单元$list里内容进行字串匹配比较 
$f=explode(" ",$list[$i]);//如果找到匹配,就以" "作为分隔符,切开留言信息$list[$i](第$i条留言),并将这些数据赋予数组$f 
$f[9]=$gb_reply;//将$f[9](留言信息最后一条数据)用$gb_reply(回复内容)代替。 
$list[$i]=$f[0]." ".$f[1]." ".$f[2]." ".$f[3]." ".$f[4]." ".$f[5]." ".$f[6]." ".$f[7]." ".$f[8]." ".$f[9]."\n";
 //将数组单元$list[$i]的内容用数组$f加上分隔符" "代替(其中$f[9]是修改了的新数据)。 
break;//跳出循环 
} 
}//循环结束符 
} 
FOR($i=0;$i<=$n;$i++){//进入循环 
fwrite($fp,$list[$i]);//将数组$list的每个单元为一行,写入文件gb.dat 
}//循环结束符 
fclose($fp);//关闭文件 
showmessage("回复成功!","index.php");//回复成功,自动返回主界面。
Copy after login

4. Data deletion program section

$list=file("gb.dat");//读取整个gb.dat文件到数组$list,数组每一个元素为一条留言($list[0]是第一条留言的数据、$list[1]是第一条留言的数据..... 
$n=count($list);//计算$list内容里的留言总数,并赋予变量$n 
if ($n>0){//如果留言数大于0 
$fp=fopen("gb.dat","w");//则以只写模式打开文件gb.dat 
for ($i=0;$i<$n;$i++) {//进入循环 
if(eregi($ttime,$list[$i])){//将发送过来发留言时间$ttime与数组$list[$i]里的字串进行匹配比较 
$list[$i]="";//如果匹配成功,则将$list[$i]清空(达到删除的目的) 
break;//跳出循环 
} 
}//循环结束符 
FOR($i=0;$i<=$n;$i++){//进入循环 
fwrite($fp,$list[$i]);//将数组$list的每个单元为一行,写入文件gb.dat 
} //循环结束符 
fclose($fp);//关闭文件 
showmessage("删除成功!","index.php");//删除成功,自动返回主界面。 
}
Copy after login

5. Data query program segment

<form action="search.php" method="post"> 
<font color="#0099CC" size="2">搜索关键字: 
<input name="found" type="text" id="found" style="background-color:#FFFFFF; color:#8888AA; border: 1 double #3399CC" size="12"> 
<input name="submit" type="image" src="image/search.gif" alt="留言搜索"> 
</font></td>
</tr> 
</table> 
</form> 
////////////////////////////////上面是搜索表单语句段 
<? 
$id=0; 
$list=file("gb.dat");//读取整个gb.dat文件到数组$list,数组每一个元素为一条留言($list[0]是第一条留言的数据、$list[1]是第一条留言的数据..... 
$n=count($list);//计算$list内容里的留言总数,并赋予变量$n 
$found=trim($found); 
if (!$found){ //如果$found为假 
echo "<center>您没有输入任何关键字!</center>";//作相关显示 
} 
else { 
if($n>0){//如果留言数大于0 
for ($i=0;$i<$n;$i++) {//进入循环 
if(eregi($found,$list[$i])){//输入的关键字与数组$list[$i]里的字串进行匹配比较 
$row=explode(" ",$list[$i]); $id=1; //如果找到匹配,就以" "作为分隔符,切开留言信息$list[$i](第$i条留言),并将这些数据赋予数组$row.并将变量$id赋予1,以便作为是否找到匹配的判断。 
list($ip,$datetime,$name,$email,$home,$face,$qq,$head,$text,$reply)=$row;//将数组$row里的单元数据按顺序赋予括号里的变量 
?> 
<img src=<? echo $head ?> >//显示客户形象图片 
<br> 
<font color="#0099CC">昵称【<? echo $name ?><font size="2">】<br>//显示客户名 
发表于:<? echo $datetime ?>//显示留言发表时间 
<br> 
<img src=<? echo $face ?>>//显示客户留言表情图片 
<? echo $name ?>说:<? echo $text; ?>//显示客户留言内容 
<br> 
<? echo $reply ?>//显示回复内容 
<br> 
<a href="<? echo $home ?>" target="_blank">访问<? echo $name ?>的主页</a>//客户主页的超连接 
<a href="mailto:<? echo $email ?>">给<? echo $name ?>发信</a>//客户E-MAIL的连接 
<? echo $name ?>的QQ号码是<? echo $qq ?>//显示客户的QQ号码 
<? echo $name ?>的IP地址为<? echo $ip ?>" //显示客户的IP地址 
<a href="reply.php?time=<? echo $datetime ?>">回复</a>//留言回复的连接语句 
<a href="del.php?time=<? echo $datetime ?>">删除</a>//留言删除的语句(以客户留言时间$datetime作为删除标识) 
<br> 
<? 
} 
}//循环结束符 
}} 
if($id==0){ echo "<center>没有找到与关键字匹配的留言!</center>";}//如果$id=0则表示没找到匹配,显示相关提示
Copy after login

Related recommendations:

Search method for PHP text database

The above is the detailed content of What are the methods of operating text databases in PHP? Five ways to operate text databases with PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!