LearningPHP-data storage and retrieval

Source: Internet
Author: User
LearningPHP-there are two basic methods to store and retrieve data: save to a common file or to a database. Data writing: 1. open the file. If the file does not exist, create it first. 2. write data into this file. 3. close the file. Data reading: 1. open the file. If this file cannot be opened, you should be aware of this and release it correctly. 2. read data from files Learning PHP-data storage and retrieval
There are two basic methods to store data: to save to a common file or to a database.
Data writing:
  • 1. open the file. If the file does not exist, create it first.
  • 2. write data into this file.
  • 3. close the file.

Data reading:
  • 1. open the file. If this file cannot be opened, you should be aware of this and release it correctly.
  • 2. read data from a file.
  • 3. close the file.

Select File mode:
When opening a file, you can choose from the following three options.
  • 1. open the file to read-only, write-only, read-and write.
  • 2. if you want to write a file, you may want to overwrite the existing file content, or simply append new data to the end. If the file already exists, terminate the program execution instead of overwriting the file.
  • 3. if you want to write a file on a system that distinguishes the second-level mode from the plain text mode, you must specify the method used.

The fopen () function supports the combination of the above three methods.

Depending on the server settings, you can get the document root directory in the following three ways:
  • $ _ SERVER ['document _ root']
  • $ DOCUMENT_ROOT
  • $ HTTP_SERVER_VARS ['document _ root']

The first style is preferred for form data.
Open a file Fopen (path, mode)
Write file: Fwrite ($ fp, & outputstring)
Close the file: Fclose ($ fp)
File mode of the fopen () function
  • R read-only mode-open the file and read from the file header
  • R + read-only mode-open a file and read/write from the file header
  • W write-only mode-open the file and read from the file header. If the file already exists, the existing content of all files will be deleted. If the file does not exist, the function will create the file.
  • X cautiously write mode open the file and start writing from the file header. If the file already exists, the file will not be opened, the fopen () function will return false, and PHP will generate a warning.
  • X + open the file in read/write mode with caution and start writing from the file header. If the file already exists, the file will not be opened, the fopen () function will return false, and PHP will generate a warning.
  • A append mode-open the file. if the file already contains content, it will be appended (written) from the end of the file. if the file does not exist, the function will create the file.
  • A + append mode-open the file. if the file already contains content, it will be appended (written) from the end of the file. if the file does not exist, the function will create the file.
  • B binary mode-used to connect to other modes. If the file system can distinguish between binary files and text files, you may use it. Maximum portability is achieved. The binary mode is the default mode.
  • T text is used in combination with other modes. This mode is only the next option in windows.

Open a file in read-only mode: Fopen ()
Know when to read the file: Feof ()
Read a row of data each time: Fgets (), Fgetss ()And Fgetcsv ()
Read the entire file: Readfile (), Fpassthru ()And File ()
  • The first method is readfile (). Readfile ($ path); call the readfile () function to open the file, output the file content to the standard output, and then close the file.
  • The second method is fpassthru (). To use this function, you must first use fopen () to open the file. Then pass the file pointer as a parameter to fpassthru (), so that the file content pointed to by the file pointer can be sent to the standard output. Then close the file. If the read operation succeeds, the function returns true; otherwise, false.
  • The third function for reading the entire file is file (). It is the same as readfile. However, it sends the result to an array.
  • $ Filearray = file ($ path );
  • The fourth option is to use the file_get_contents () function. This function is the same as readfile (), but it returns the file content in the form of a string instead of displaying the file content in the browser.

Read one character: Fgetc ()
while(!feof($fp)){     $char = fgetc($fp);     if(!feof($fp)){          echo ($char == "\n "? "
": $char); }}

Read arbitrary length: f Read ()
The last way to read a file is to use the fread () function to read any length of bytes from the file.
Check whether the file exists: File_exists ()
Determine the file size: Filesize ()
Delete an object: Unlink ()(PHP does not have a function named delete)
Locate in the file: Rewind (), Fseek ()And Ftell ()
Rewind ()The function can reset the pointer to the beginning of the file.
Ftell ()The function reports the position of the file pointer in the file in bytes.
Call Fseek ()The function can move the file pointer fp from The whence position offset byte.
Rewind ()The function is equivalent to calling a fseek () function with zero offset.
File lock:
To prevent multiple methods from simultaneously operating a file, you can use the file locking method.
File locking is implemented through the flock () function.
If you want to use the flock () function, you must add it to all scripts that use files; otherwise, it makes no sense.
Flock () operation value
  • LOCK_SH: the read operation is locked. This means that files can be shared and others can read the files.
  • The LOCK_EX write operation is locked. This is mutually exclusive. The file cannot be shared.
  • LOCK_UN release existing locks
  • LOCK_NB prevents blocking during request locking


Database management system
  • RDBMS provides faster data access than normal files.
  • RDBMS can easily search for and retrieve data sets that meet specific conditions.
  • RDBMS has a built-in mechanism for processing concurrent access.
  • RDBMS can access data randomly.
  • RDBNS has a built-in permission system.


Vieworders. php
 Bob's Auto Parts - Customer OrdersBob's Auto PartsCustomer Orders
 No orders pending.Please try again later.

";exit;}while (!feof($fp)){$order = fgets($fp,999);echo $order."
";}?>

Proccessorder. php
 Bob's Auto Parts-Order ResultsBob's Auto PartsOrder Results
 Order processed at ".date('H:i, jS F Y')."

";echo "

Your order is as follows:

";$totalqty = 0;$totalqty = $tireqty + $oilqty + $sparkqty;echo "Items ordered: ".$totalqty."
";if($totalqty == 0){ echo "You did not order anything on the previous page!
";}else{if($tireqty > 0){ echo $tireqty." tires
"; }if($oilqty > 0){ echo $oilqty." bottles of oil
"; }if($sparkqty > 0){ echo $sparkqty." spark plugs
"; }}$totalamount = 0.00;define("TIREPRICE", 100);define("OILPRICE", 10);define("SPARKPRICE", 4);$totalamount = $tireqty * TIREPRICE+ $oilqty * OILPRICE+ $sparkqty * SPARKPRICE;$totalamount=number_format($totalamount, 2, '.', ' ');echo "

Total of order is $.$totalamount.

";echo "

Address to ship to is ".$address."

";$outputstring = $date."\t".$tireqty." tires \t".$oilqty." oil\t".$sparkqty." spark plugs\t\$".$totalamount."\t".$address."\n";@ $fp= fopen("$DOCUMENT_ROOT/orders/orders.txt", 'ab');flock($fp, LOCK_EX);if(!$fp){echo "

Your orde could not be processed at this time. Please try again later.

";exit;}fwrite($fp, $outputstring, strlen($outputstring));flock($fp, LOCK_UN);fclose($fp);echo "

Order written.

";?>

References:
PHP & MySQL. Web







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.