


What are the methods of operating text databases in PHP? Five ways to operate text databases with PHP
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秒后自动返回主界面。
$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>";//如果文件不存在显示的信息 }
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");//回复成功,自动返回主界面。
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");//删除成功,自动返回主界面。 }
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则表示没找到匹配,显示相关提示
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
