PHP Database Access Introduction - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Database Access Introduction - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript


Post Top Ad

Post Top Ad

Thursday, June 6, 2019

PHP Database Access Introduction

PHP Database Access


Databases are central to many web applications. A database can hold almost any collection of information you may want to search and update, such as a user list, a product catalog, or recent headlines. One reason why PHP is such a great web programming language is its extensive database support. PHP can interact with just about any database you can think of, some relational and some not. It also has ODBC support, so even if your favorite database isn’t in the list, as long as it supports ODBC, you can use it with PHP.

DBM databases, are simple, robust, and efficient flat files but limit the structure of your data to key/value pairs. If your data can be organized as a mapping of keys to values, DBM databases are a great choice.

PHP really shines, though, when paired with an SQL database. This combination is used for most of the recipes in this chapter. SQL databases can be complicated, but they are extremely powerful. To use PHP with a particular SQL database, PHP must be explicitly told to include support for that database when it is compiled. If PHP is built to support dynamic module loading, the database support can also be built as a dynamic module.

The SQL database examples in this chapter use PHP 5’s PDO database access layer. With PDO, you use the same PHP functions no matter what database engine you’re talking to. Although the syntax of the SQL may differ from database to database, the PHP code remains similar. In this regard, PDO offers data access abstraction, not total database abstraction. There are other libraries that attempt to solve the total database abstraction problem—they hide the implementation details of different databases such as date handling and column types behind a layer of code. Although this sort of abstraction can save you some work if you’re writing software that is intended to be used with lots of different types of databases, it can cause other problems. When you write SQL focused on a particular type of database, you can take advantage of that database’s features for maximum performance.

PHP 5 comes bundled with SQLite, a powerful database that doesn’t require a separate server. It’s a great choice when you have a moderate amount of traffic and don’t want to deal with the hassles of running a database server. Discusses the ins and outs of SQLite.

Example  Sample table structure

        CREATE TABLE zodiac (
              id INT UNSIGNED NOT NULL,
              sign CHAR(11),
              symbol CHAR(13),
              planet CHAR(7),
              element CHAR(5),
              start_month TINYINT,
              start_day TINYINT,
              end_month TINYINT,
              end_day TINYINT,
              PRIMARY KEY(id)

Example  Sample table data

        INSERT INTO zodiac VALUES (1,'Aries','Ram','Mars','fire',3,21,4,19);
        INSERT INTO zodiac VALUES (2,'Taurus','Bull','Venus','earth',4,20,5,20);
        INSERT INTO zodiac VALUES (3,'Gemini','Twins','Mercury','air',5,21,6,21);
        INSERT INTO zodiac VALUES (4,'Cancer','Crab','Moon','water',6,22,7,22);
        INSERT INTO zodiac VALUES (5,'Leo','Lion','Sun','fire',7,23,8,22);
        INSERT INTO zodiac VALUES (6,'Virgo','Virgin','Mercury','earth',8,23,9,22);
        INSERT INTO zodiac VALUES (7,'Libra','Scales','Venus','air',9,23,10,23);
        INSERT INTO zodiac VALUES (8,'Scorpio','Scorpion','Mars','water',10,24,11,21);
        INSERT INTO zodiac VALUES (9,'Sagittarius','Archer','Jupiter','fire',11,22,12,↵
        INSERT INTO zodiac VALUES (10,'Capricorn','Goat','Saturn','earth',12,22,1,19);
        INSERT INTO zodiac VALUES (11,'Aquarius','Water Carrier','Uranus','air',1,20,2,↵
        INSERT INTO zodiac VALUES (12,'Pisces','Fishes','Neptune','water',2,19,3,20);

Discusses how to connect to a database, the code in the subsequent recipes omits those lines so they can focus on the specifics of queries and result handling.

Typical PHP programs capture information from HTML form fields and store that information in the database. Some characters, such as the apostrophe and backslash, have special meaning in SQL, so you have to be careful if your form data contains those characters.

Versions of PHP prior to 5.4.0 have a feature called magic quotes that attempts to make this easier. When the configuration setting magic_quotes_gpc is on, variables coming from get requests, post requests, and cookies have single quotes, double quotes, backslashes, and nulls escaped with a backslash. You can also turn on magic_quotes_run time to automatically escape quotes, backslashes, and nulls from external sources such as database queries or text files. For example, if magic_quotes_runtime is on and you read a file into an array with file(), the special characters in that array are backslash escaped.

Unfortunately, magic quotes usually turns out to be more like annoying quotes. If you want to use submitted form data in any other context than an SQL query (for example, displaying it in a page), you need to undo the escaping so the page looks right. If you’re using a version of PHP before 5.4.0, set the various magic quotes–related configuration directives mentioned to off. The right way to handle proper escaping of user input for database queries, which explains PDO’s bound parameters support. Additionally, discusses escaping special characters in queries in more detail. General debugging techniques you can use to handle errors resulting from database queries.

The next set of recipes cover database tasks that are more involved than just simple queries. Recipe 10.11 shows how to automatically generate unique ID values you can use as record identifiers. Recipe 10.12 covers building queries at runtime from a list of fields. This makes it easier to manage INSERT and UPDATE queries with a lot of columns. Demonstrates how to display links that let you page through a result set, displaying a few records on each page. To speed up your database access, you can cache queries and their results.

Shows techniques for managing access to a single database connection from multiple places in a large program. Then, ties together some of the topics discussed in the chapter in a complete program that stores a threaded message board in a database.

In addition to SQL databases, PHP can work with a large number of so-called NoSQL databases—data stores that offer different models of how you organize and query for your information. There are too many NoSQL databases out there to cover them all here, so we talk about one, Redis.

No comments:

Post a Comment

Post Top Ad