|
Chapter 12
Storing Data with PHP
In This Chapter
Writing and reading flat files
Exchanging data between PHP and other programs
Understanding database support in PHP
Using PHP to interact with a database
Handling database-connection errors
M
any applications require the long-term storage of information. In PHP
scripts, you can make information available within sessions— periods
of time that users spend at your Web site — by using methods such as PHP
session functions and by submitting forms. However, eventually you need to
store information for use tomorrow or next week. You can store it in a cookie
that you set to last after the session is ended (as discussed in Chapter 11), but
the information is vulnerable. It’s not under your control. The user can delete
or change the information at any time or can refuse to accept the cookie. To be
available and stable, the information needs to be stored somewhere secure,
where no one can access or tamper with it. The information needs to be stored
on the server.
Information can be stored on the server in flat files or in databases. Flat files
are text files stored in the computer file system. Humans can read flat files by
using the operating system commands that display files, such as
cat
in Linux
and Unix. You can access and edit these files by using any text file editor, such
as Notepad or vi. The information in the flat file is stored as strings, and the
PHP script that retrieves the data needs to know how the data is stored. For
example, to retrieve a customer name from a file, the PHP script needs to
know that the customer name is stored in the first 20 characters of every line.
Using a database for data storage requires you to install and learn to use data-
base software, such as MySQL or Oracle. The data is stored in files created by
the database software and can only be accessed by the database software.
Databases can store very complex information that you can retrieve easily.
|