Home php教程 php手册 PHP实现对文本数据库的五大基本操作的方法

PHP实现对文本数据库的五大基本操作的方法

Jun 13, 2016 am 10:20 AM
http php Basic operations accomplish right database text method source of

来源: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$page=1;//则赋值为1
}
$number=($page-1)*$pagesize;//计算当前所显示第一个留言的数码号(数码号从零开始,主要是达到与数组单元号对应的目的)
for($i=0;$i$row=explode("|",$arr[$number]);//以"|"作为分割符,分割数组$arr中每第$number个单元的数据,并将这些数据赋予数组$rom
list($ip,$datetime,$name,$email,$home,$face,$qq,$head,$text,$reply)=$row;//将数组$row里的单元数据按顺序赋予括号里的变量
?>
PHP实现对文本数据库的五大基本操作的方法 >//显示客户形象图片


昵称【 echo $name ?>
//显示客户名
发表于: echo $datetime ?>//显示留言发表时间


PHP实现对文本数据库的五大基本操作的方法>//显示客户留言表情图片
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、数据修改程序段
$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;$iif(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]." "; //将数组单元$list[$i]的内容用数组$f加上分隔符"|"代替(其中$f[9]是修改了的新数据)。
break;//跳出循环
}
}//循环结束符
}
FOR($i=0;$ifwrite($fp,$list[$i]);//将数组$list的每个单元为一行,写入文件gb.dat
}//循环结束符
fclose($fp);//关闭文件
showmessage("回复成功!","index.php");//回复成功,自动返回主界面。

4、数据删除程序段
$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;$iif(eregi($ttime,$list[$i])){//将发送过来发留言时间$ttime与数组$list[$i]里的字串进行匹配比较
$list[$i]="";//如果匹配成功,则将$list[$i]清空(达到删除的目的)
break;//跳出循环
}
}//循环结束符
FOR($i=0;$ifwrite($fp,$list[$i]);//将数组$list的每个单元为一行,写入文件gb.dat
} //循环结束符
fclose($fp);//关闭文件
showmessage("删除成功!","index.php");//删除成功,自动返回主界面。
}


5、数据查询程序段


搜索关键字:






////////////////////////////////上面是搜索表单语句段

$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 "
您没有输入任何关键字!
";//作相关显示
}
else {
if($n>0){//如果留言数大于0
for ($i=0;$iif(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里的单元数据按顺序赋予括号里的变量
?>
PHP实现对文本数据库的五大基本操作的方法 >//显示客户形象图片


昵称【 echo $name ?>
//显示客户名
发表于: echo $datetime ?>//显示留言发表时间


PHP实现对文本数据库的五大基本操作的方法>//显示客户留言表情图片
echo $name ?>说: echo $text; ?>//显示客户
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

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.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

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: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

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 vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

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.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

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.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

See all articles