Five basic PHP operations on text databases-PHP Tutorial

Source: Internet
Author: User
PHP has five basic operations on text databases. PHP implements five basic operations on the Text Database: data display, addition, modification, deletion, and query. I use a message book program as an example, describes how PHP implements five basic operations on Text Database: data display, addition, modification, deletion, and query.

I used a message book program as an example to illustrate how PHP can display, add, modify, delete, and query data in a text database.

This text database has 10 fields: customer IP address, speech time, customer name, customer EMAIL, customer homepage address, message emoticons image name, customer QQ, customer Image image, message content, administrator reply content.

1. add the data program segment.

$ Date = date ("Y-m-d H: I: s"); // Obtain the system time
$ Ip = $ HTTP_SERVER_VARS [REMOTE_ADDR]; // obtain the ip address of the speaker.
$ Text = encode ($ gb_text); // Remove the space after the message content.
$ Fp = fopen ("gb. dat", "a"); // open the gb. dat text file in write-only mode. the file pointer points to the end of the file.
$ Str = $ ip. "| ". $ date. "| ". $ gb_name. "| ". $ gb_email. "| ". $ gb_home. "| ". $ face. "| ". $ gb_qq. "| ". $ head. "| ". $ text. "| ". $ reply. "n"; // assign all message data to the variable $ str, "|", which is used as the data interval symbol for data segmentation in the future.
Fwrite ($ fp, $ str); // write data to a file
Fclose ($ fp); // close the file
Showmessage ("message successful! "," Index. php "," 3 "); // The message is sent successfully. The message is returned to the main interface three seconds later.
$ Gb_name, $ gb_email, $ gb_home, $ face, $ gb_qq, $ head, $ gb_text, and $ reply are data transmitted from the form.

2. data display section

If (file_exists ("gb. dat") {// check whether the file exists
$ Array = file ("gb. dat"); // read all the file content into the array $ array
$ Arr = array_reverse ($ array); // arrange the data in $ array in a reverse manner (that is, the last row is the first row, and so on) read every unit of the array $ arr ($ arr [0]...).
$ Num = count ($ array); // gets the number of information in the array $ array (one row is an item)
If ($ num> 0) {// if the number of information is greater than zero (that is, the text database is not empty)
$ Total = ceil ($ num/$ pagesize); // calculates the total number of pages (take the maximum integer, that is, any number of decimal points is rounded to an integer, $ pagesize indicates the number of information displayed on each page)
If ($ page <1) {// if the current page digital number is less than 1
$ Page = 1; // The value is 1.
}
$ Number = ($ page-1) * $ pagesize; // calculate the number of the first message displayed currently (the number starts from scratch, this mainly aims to correspond to the array unit number)
For ($ I = 0; $ I <= $ pagesize-1; $ I) {// enter the loop
$ Row = explode ("|", $ arr [$ number]); // use "|" as the delimiter to split the data of every $ number unit in the array $ arr, and assign the 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 sequence.
?>
> // Display the customer Image


Nickname 【 ]
// Display the customer name
Posted on: // Display the message posting time


> // Display the customer's message and emoticons
Said: // Display the customer's message content


// Display the reply content


"Target =" _ blank "> access Homepage // hyperconnections of the customer homepage
"> Mail/client E-MAIL connection
The QQ number is // Display the customer's QQ number
The IP address of IS "// Display the customer's IP address
"> Reply // connection statement for message Reply
"> Delete // the message deletion statement (with the customer message time $ datetime as the deletion Identifier)


If ($ number = $ num-1) // if the cell number of the array is equal to the total number of messages minus one (because the cell number starts from zero, it means this is the last message)
{
Break; // skip loop
}
$ Number = $ number 1; // array unit number plus 1

Using a message-based program as an example, I will explain how PHP implements the text database...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.