It is a good idea to be familiar with the methods that are made
available to you through the PDO class??”you don??™t have to understand exactly how they work,
but knowing that the functionality is already available can save you hours of painstakingly reinventing
the wheel.
CHAPTER 4 ?– CREATING THE PRODUCT CATALOG: PART 1 86
?– Note In this book, you??™ll learn about the PHP PDO functionality as used in TShirtShop. For more details
about PHP PDO, see the PHP Manualdocumentation at http://www.php.net/manual/en/ref.pdo.php.
The PDO class provides the functionality to connect to the MySQL server and execute SQL
queries. The method that opens a database connection is PDO??™s constructor, which receives as
parameters the connection string to the database server and an optional parameter that specifies
whether the connection is a persistent connection. The connection string contains the data
required to connect to the database server. You create a new PDO object like this:
$dbh = new PDO('mysql:dbname=' . $db_name . ';host=' . $db_host,
$db_user,
$db_pass,
array(PDO::ATTR_PERSISTENT => $persistent));
?– Note The constructor of the PDO class returns an initialized database connection object (which is specific
to the type of database you??™re connecting to, such as mysql) if the connection is successful; otherwise, an
exception is thrown.
The previous code snippet shows the standard data you need to supply when connecting
to aMySQL server and uses five variables:
??? $db_user represents the username.
Pages:
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168