chrisgreen1993/snippetshare — pull request #4 by robmcvey (2012-06-09 21:57:13+00:00)
Title: updated to PDO for database queries, more "MVC" folder structure

### New folder structure

[](https://dl.dropbox.com/u/12520837/Screen%20shot%202012-06-09%20at%2022.38.21.png)

Moved more towards an "MVC" pattern. Also, if you ever need more models (maybe you make a users table in the database?) instead of adding more and more methods to your old `model.php` file, you create a `User.php` class which can then sub-class (extend) from the Mysql class.

Use the config file for site wide (but not database specific) things like times, dates, currency, cacheing, paths, etc.

Use the database.php for database connections.

### Changed to PDO for database queries

See http://www.php.net/manual/en/book.pdo.php

The `mysqli` extension is not installed by default - PDO is a better option, adds flexibility (maybe one day you want to switch to Oracle!? In which case you wouldn't have to change much)

### Exceptions

You'll notice I've used Exceptions in most of the database methods.

The PDO class will throw an exception if something doesn't work, you can then catch this error and show the user.

When an exception is thrown, everything else after the point it was throw inside the try block is not run, so dealing with different errors inside the same method is alot easier e.g.

#### Without exceptions

````php
function foo($bar) {
if($bar == 'apple') {
$error = 'i dont like apples';
}
elseif($bar == 'tomato') {
$error = 'i dont like tomoates';
}
else {
$success = 'i like most other stuff';
}

// We still have to check if error happened!
if(!isset($success)) {
return $error;
} else {
return $success;
}
}
````
#### With exceptions

````php
function foo($bar) {
try {
if($bar == 'apple') {
throw new Exception('i dont like apples');
}
elseif($bar == 'tomato') {
throw new Exception('i dont like tomato');
}

return 'i like most other stuff';

} catch(Exception $e) {
return $e->getMessage();
}
}
````