The methods that execute database stored procedures have a standard structure, taking advantage of the fact that
PDO has been configured to throw exceptions. Let??™s take a closer look at the GetRow() method:
// Wrapper method for PDOStatement::fetch()
public static function GetRow($sqlQuery, $params = null,
$fetchStyle = PDO::FETCH_ASSOC)
{
// Initialize the return value to null
$result = null;
// Try to execute an SQL query or a stored procedure
try
{
// Get the database handler
$database_handler = self::GetHandler();
// Prepare the query for execution
$statement_handler = $database_handler->prepare($sqlQuery);
CHAPTER 4 ?– CREATING THE PRODUCT CATALOG: PART 1 97
// Execute the query
$statement_handler->execute($params);
// Fetch result
$result = $statement_handler->fetch($fetchStyle);
}
// Trigger an error if an exception was thrown when executing the SQL query
catch(PDOException $e)
{
// Close the database handler and trigger an error
self::Close();
trigger_error($e->getMessage(), E_USER_ERROR);
}
// Return the query results
return $result;
}
This method generates an error (using the trigger_error() function) if the database command didn??™t execute
successfully. The error is captured by the error-handling mechanism you implemented in Chapter 3.
Because of the way you implemented the error-handling code in Chapter 3, generating an E_USER_ERROR error
freezes the execution of the request, eventually logging and/or e-mailing the error data, and showing the visitor
a nice ???Please come back later??? message (if there is such thing as a nice ???Please come back later??? message,
anyway).
Pages:
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181