Examples of common PHP operations on text databases. PHP can display, add, modify, delete, and query data in a text database. Let's take a message-based program as an example to describe how PHP can display, add, modify, delete, and query data in a text database.
Let's take a message-based program as an example to describe 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 a data program:
$ Date = date ("Y-m-d H: I: s"); // Obtain the system time $ ip = $ HTTP_SERVER_VARS [REMOTE_ADDR]; // get the IP address $ text = encode ($ gb_text); // Remove the space behind the message content. $ fp = fopen ("gb. dat "," a "); // open gb in write-only mode. dat text file. 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. ""; // 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 the file fclose ($ fp); // close the file showmessage ("message success! "," 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 program:
<? If (file_exists ("gb. dat ") {// check whether the file exists $ array = file (" gb. dat "); // read all the file content into the array $ arr = array_reverse ($ array ); // arrange the data security lines in $ array (that is, when the last row is the first row, and so on) read every unit of the array $ arr ($ arr [0]...). $ Num = count ($ array); // Obtain the number of information in the array $ array (one row is an information) if ($ num> 0) {// if the number of messages is greater than zero (that is, the text database is not empty) $ total = ceil ($ num/$ pagesize); // calculates the total number of pages (maximum integer, that is, if all decimal points are rounded to an integer, $ pagesize indicates the number of information displayed on each page.) if ($ page <1) {// if the number of the current page is less than 1 $ page = 1; // The value is 1} $ number = ($ page-1) * $ pagesize; // calculate the number of the first message currently displayed (the number starts from scratch, mainly to match 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, $ da Tetime, $ 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 [<? Echo $ name?>]
// Display the customer name in: <? Echo $ datetime?> // Display the message posting time
> // Display the customer's message and emoticons <? Echo $ name?> Said: <? Echo $ text;?> // Display the customer's message content
<? Echo $ reply?> // Display the reply content
"Rel =" external nofollow "target =" _ blank "> access <? Echo $ name?> Homepage // hyperconnection "rel =" external nofollow "> to <? Echo $ name?> Mail // client E-MAIL connection <? Echo $ name?> The QQ number is <? Echo $ qq?> // Display the customer's QQ number <? Echo $ name?> The IP address of is <? Echo $ ip?> "// Display the customer's IP address" rel = "external nofollow"> Reply // message reply connection statement "rel =" external nofollow "> delete // message deletion statement (customer Message time $ datetime as the deletion Identifier)
<? If ($ number = $ num-1) // if the unit number of the array is equal to the total number of messages minus one (because the unit number starts from zero, so this means this is the last message) {break; // skip loop} $ number = $ number + 1; // array unit number plus 1} // Loop Terminator} if ($ page <> 1) // if the current page digital number is not equal to 1 {$ back = $ page-1; // if the current page digital number is reduced by 1, and assign this value to the variable $ backecho ""; // display the connection echo "" on the first page; the current page digital number is equal to $ back, and display its connection} if ($ page <> $ total) // if the current page number is not equal to the total page number (the last page number) {$ next = $ page + 1; // add the current page number to 1 and assign the variable $ nextecho "next page"; // display the next page Connection echo "last page "; display last page connection} echo "page number: $ page/$ total"; // display the current page number and the last page number echo "$ num messages in total "; // Display message count} else {echo"
There are no messages currently!
"; // Information displayed if the file content is empty} else {echo"
Data File loss. please contact the administrator! Or publish a message to recreate the data file!
"; // If the file does not exist, display the information}
3. data modification program:
$ List = file ("gb. dat "); // read the entire gb. the dat file goes to the array $ list. each element of the array is a message ($ list [0] is the data of the first message, and $ list [1] is the data of the first message ..... $ n = count ($ list); // calculates the total number of messages in the $ list content, and assigns the variable $ nif ($ n> 0) {// if the number of messages is greater than 0 $ fp = fopen ("gb. dat "," w "); // open the file gb in write-only mode. dat $ gb_reply = encode ($ gb_reply); for ($ I = 0; $ I <$ n; $ I ++) {// enter the loop if (eregi ($ ttime, $ list [$ I]) {// match the message sending time $ ttime with the content in the array unit $ list $ f = explode ("| ", $ list [$ I]); // if a match is found, the "|" separator is used to cut the message $ list [$ I] (Message $ I ), and assign the data Array $ f [9] = $ gb_reply; // replace $ f [9] 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 content of the array unit $ list [$ I] with the separator "|" in array $ f ($ f [9] is the new data modified ). Break; // skip loop} // Loop Terminator} FOR ($ I = 0; $ I <= $ n; $ I ++) {// enter the Loop fwrite ($ fp, $ list [$ I]); // write each unit of the array $ list as one row into the file gb. dat} // Loop Terminator fclose ($ fp); // close the file showmessage ("reply successful! "," Index. php "); // if the response is successful, the system automatically returns to the main interface.
4. data deletion program:
$ List = file ("gb. dat "); // read the entire gb. the dat file goes to the array $ list. each element of the array is a message ($ list [0] is the data of the first message, and $ list [1] is the data of the first message ..... $ n = count ($ list); // calculates the total number of messages in the $ list content, and assigns the variable $ nif ($ n> 0) {// if the number of messages is greater than 0 $ fp = fopen ("gb. dat "," w "); // open the file gb in write-only mode. datfor ($ I = 0; $ I <$ n; $ I ++) {// enter the loop if (eregi ($ ttime, $ list [$ I]) {// compare the sent message sending time $ ttime with the strings in the array $ list [$ I] $ list [$ I] = ""; // if the match succeeds, the $ list [$ I] is cleared (for the purpose of deletion) break; // skip loop} // Loop Terminator FOR ($ I = 0; $ I <= $ n; $ I ++) {// enter the Loop fwrite ($ f P, $ list [$ I]); // write each cell of the array $ list into a row and into the file gb. dat} // Loop Terminator fclose ($ fp); // close the file showmessage ("deleted successfully! "," Index. php "); // if the deletion is successful, the system automatically returns to the main interface .}
5. Data Query program:
//// // The above is the search form statement segment <? $ Id = 0; $ list = file ("gb. dat "); // read the entire gb. the dat file goes to the array $ list. each element of the array is a message ($ list [0] is the data of the first message, and $ 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 $ found = trim ($ found); if (! $ Found) {// if $ found is false echo"
You have not entered any keywords!
"; // Related display} else {if ($ n> 0) {// if the number of messages is greater than 0for ($ I = 0; $ I <$ n; $ I ++) {// enter the loop if (eregi ($ found, $ list [$ I]) {// match the entered keyword with the string in the array $ list [$ I] $ row = explode ("|", $ list [$ I]); $ id = 1; // if a match is found, use "|" as the separator to cut the message $ list [$ I] (Message $ I ), and assign the data to the array $ row. and assign the variable $ id to 1 to determine whether matching is found. 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 [<? Echo $ name?>]
// Display the customer name in: <? Echo $ datetime?> // Display the message posting time
> // Display the customer's message and emoticons <? Echo $ name?> Said: <? Echo $ text;?> // Display the customer
Bytes. Let's take a message book program as an example to briefly describe the PHP implementation of the number of texts...