TAILIEUCHUNG - OBJECT-ORIENTED PHP Concepts, Techniques, and Code- P19

OBJECT-ORIENTED PHP Concepts, Techniques, and Code- P19:A number of years ago, before I started using PHP, I created dynamic web pages using C. This really wasn’t too different from some of the other options available at the time, though it seems almost unthinkable now. Creating a dynamic page meant outputting HTML from your script and recompiling that script if any changes needed to be made. | unbuffered behavior is easily reproduced. Our SQLite application uses buffered result sets in cases where we need to know that there are records returned or where the specific number of records is required. To buffer records using PDO you can use the fetchAll method of a statement to return an array. A record count can be obtained by using the count function on the returned array. Alternately calling the function empty on the statement returned by a query will determine whether there is at least one record. In general when querying the database it looks like some efficiencies have been lost. What is a single process using SQLite becomes a two-step process with PDO. Using two methods instead of one can make code more complicated. However as we ll soon see there are some important advantages to the PDO methods and in some cases this two-step process can be simplified. Additional Capabilities of PDO Converting one application certainly doesn t tell the whole story about PDO so let s have a look at some of the other capabilities of PDO. There are three PDO classes PDO the database or connection class PDOStatement and PDORow. By far the most interesting and unfamiliar class is the statement class and this is where we ll concentrate our attention. We ll briefly discuss PDORow when we come to the fetchObject method of the statement class. The PDO Class So far in this book we ve created our own connection class and used the SQLiteDatabase class classes that have many similarities to PDO. With this experience I needn t say a lot about the PDO class. I ve already mentioned the quote setAttribute and query methods of the PDO class. For databases such as SQLite that support transactions this class also has methods to begin commit or roll back transactions. The most important method however is prepare. This method is similar to the query method in that it also returns a PDOStatement. The major difference is that query is typically used for SQL statements that are issued once .

19    16    0
182    13    0
138    6    0
3    7    0
142    12    0