Add the GetOne() method, which is the wrapper class for PDOStatement::fetch(), as shown. This will
be used to get a single value resulted from a SELECT query:
// Return the first column value from a row
public static function GetOne($sqlQuery, $params = null)
{
// 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);
// Execute the query
$statement_handler->execute($params);
// Fetch result
$result = $statement_handler->fetch(PDO::FETCH_NUM);
/* Save the first value of the result set (first column of the first row)
to $result */
$result = $result[0];
}
// 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;
}
8. Create a file named catalog.php file inside the business folder. Add the following code into this file:
// Business tier class for reading product catalog information
CHAPTER 4 ?– CREATING THE PRODUCT CATALOG: PART 1 95
class Catalog
{
// Retrieves all departments
public static function GetDepartments()
{
// Build SQL query
$sql = 'CALL catalog_get_departments_list()';
// Execute the query and return the results
return DatabaseHandler::GetAll($sql);
}
}
?>
9.
Pages:
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178