


PHP methods to implement five basic operations on text database_PHP tutorial
来源:http://www.pchoer.com/
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")){//检测文件是否存在
$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里的单元数据按顺序赋予括号里的变量
?> >//显示客户形象图片
昵称【 echo $name ?>】
//显示客户名
发表于: echo $datetime ?>//显示留言发表时间
>//显示客户留言表情图片
echo $name ?>说: echo $text; ?>//显示客户留言内容
echo $reply ?>//显示回复内容
访问 echo $name ?>的主页//客户主页的超连接
给 echo $name ?>发信//客户E-MAIL的连接
echo $name ?>的QQ号码是 echo $qq ?>//显示客户的QQ号码
echo $name ?>的IP地址为 echo $ip ?>" //显示客户的IP地址
回复//留言回复的连接语句
删除//留言删除的语句(以客户留言时间$datetime作为删除标识)
if ($number == $num-1)//如果数组的单元号等于总留言数减一(因为单元号以零开始的,所以这意味着这是最后一条留言)
{
break;//跳出循环
}
$number = $number + 1; //数组单元号加1
}//循环结束符
}
if ($page <> 1)//如果当前页面数码号不等于1
{
$back = $page - 1;//当前页面数码号减1,并将此值赋予变量$back
echo "第一页";//显示第一页的连接
echo " 上一页" ;当前页面数码号等于$back,并显示其连接
}
if ($page <> $total)//如果当前页面数码号不等于总页数号(最后一页数码号)
{
$next = $page + 1;//当前页面数码号加1并赋予变量$next
echo " 下一页" ;//显示下一页连接
echo " 最后一页"; 显示最后一页连接
}
echo "页数:$page / $total";//显示当前页面数码号和显示最后一页数码号
echo "共有 $num 条留言";//显示留言数信息
}
else {
echo "
}
else {
echo "
}
3. Data modification program segment
$list=file("gb.dat");//Read the entire gb.dat file into the array $list, each element of the array is a message ($list[0 ] is the data of the first message, $list[1] is the data of the first message....
$n=count($list);//Calculate the total number of messages in the $list content, and Assign the variable $n
if ($n>0){ //If the number of messages is greater than 0
$fp=fopen("gb.dat","w");//Open the file in write-only mode gb.dat
$gb_reply=encode($gb_reply);
for ($i=0;$i<$n;$i++) {//Enter the loop
if(eregi($ttime,$ list[$i])){//Compare the string matching time $ttime sent to send a message with the content in the array unit $list
$f=explode("|",$list[$i]); //If a match is found, use "|" as the delimiter to cut the message information $list[$i] (the $ith message), and assign these data to the array $f
$f[9]= $gb_reply;//Replace $f[9] (the last piece of data in the message) with $gb_reply (reply content)
$list[$i]=$f[0]."|".$f[. 1]."|".$f[2]."|".$f[3]."|".$f[4]."|".$f[5]."|".$f[ 6]."|".$f[7]."|".$f[8]."|".$f[9]."
"; //Replace the contents of the array unit $list[$i] with the array $f plus the separator "|" (where $f[9] is the modified new data).
break; // Jump out Loop
}
}//Loop end character
}
FOR($i=0;$i<=$n;$i++){//Enter loop
fwrite($fp ,$list[$i]);//Create each unit of the array $list into one line and write it to the file gb.dat
}//Loop end character
fclose($fp);//Close the file
showmessage("Reply successfully!","index.php");//Reply successfully and automatically return to the main interface
4. Data deletion program segment
$list=file("gb.dat");//Read the entire gb.dat file into the array $list, each element of the array is a message ($list[0 ] is the data of the first message, $list[1] is the data of the first message....
$n=count($list);//Calculate the total number of messages in the $list content, and Assign the variable $n
if ($n>0){//If the number of messages is greater than 0
$fp=fopen("gb.dat","w");//Open the file in write-only mode gb.dat
for ($i=0;$i<$n;$i++) {//Enter the loop
if(eregi($ttime,$list[$i])){//Will send Come and send a message. The time $ttime is matched with the strings in the array $list[$i]
$list[$i]="";//If the match is successful, $list[$i] will be cleared ( To achieve the purpose of deletion)
break;//Jump out of the loop
}
}//Loop end character
FOR($i=0;$i<=$n;$i++){// Enter the loop
fwrite($fp,$list[$i]);//Convert each unit of the array $list into a line and write it to the file gb.dat
} //Loop end character
fclose ($fp);//Close the file
showmessage("Delete successfully!","index.php");//Delete successfully and automatically return to the main interface.
}
5. Data query program segment
/////////////////////// //////////The above is the search form statement segment
$id=0;
$list=file("gb.dat");//Read the entire gb.dat file to the array $list, each element of the array is a message ($list[0] is the data of the first message, $list[1] is the data of the first message....
$n=count($list);//Calculate the total number of messages in the $list content and assign it to the variable $n
$found=trim($found);
if (!$found){ //If $found is false
echo "
}
else {
if($n>0 ){//If the number of messages is greater than 0
for ($i=0;$i<$n;$i++) {//Enter the loop
if(eregi($found,$list[$i]) ){//The entered keywords are matched and compared with the strings in the array $list[$i]
$row=explode("|",$list[$i]); $id=1; // If a match is found, use "|" as the delimiter to cut the message information $list[$i] (the $ith message), and assign these data to the array $row. And assign 1 to the variable $id so as to Determine whether a match is found.
list($ip,$datetime,$name,$email,$home,$face,$qq,$head,$text,$reply)=$row;//Change the array $ The unit data in the row is assigned to the variables in the brackets in order
?>
Nickname【 echo $name ?>】
//Display customer name
Posted in : echo $datetime ?>//Display the message publishing time
echo $name ?>Say: echo $text; ?>//Show customer

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











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
