Using try/catch blocks
// without try/catch
<?php
// model.php
function get_all_posts()
{
$link = mysql_connect('localhost', 'myuser', 'mypassword');
if ($link == FALSE || mysql_select_db('blog_db', $link);)
{
echo "Error al conectarse";
}
$result = mysql_query('SELECT id, title FROM post', $link);
if ($result == FALSE)
{
echo "Error en query!";
return;
}
$posts = array();
while ($row = mysql_fetch_assoc($result)) {
$posts[] = $row;
}
mysql_close($link);
return $posts;
}
?>-----------------------------
// with try/catch
<?php
// model.php
function get_all_posts()
{
try {
$link = mysql_connect('localhost', 'myuser', 'mypassword');
mysql_select_db('blog_db', $link);
$result = mysql_query('SELECT id, title FROM post', $link);
$posts = array();
while ($row = mysql_fetch_assoc($result)) {
$posts[] = $row;
}
mysql_close($link);
}
catch (Exception $e) {
echo "Error al obtener los posts!";
echo $e->getMessage();
}
return $posts;
}
Abstract DB details
<?php
// model.php
require_once "lib/database.php";
function get_all_posts()
{
$link = open_database_connection();
$result = query('SELECT id, title FROM post', $link);
$posts = array();
while ($row = fetch_assoc($result)) {
$posts[] = $row;
}
close_database_connection($link);
return $posts;
}
?>-----------------------------
<?php
// lib/database.php
function open_database_connection()
{
$link = mysql_connect('localhost', 'myuser', 'mypassword');
mysql_select_db('blog_db', $link);
return $link;
}
function close_database_connection($link)
{
mysql_close($link);
}
function query($query, $dblink(
{
return mysql_query($query, $dblink);
}
function fetch_assoc($resultset)
{
return mysql_fetch_assoc($resultset);
}
Abstract DB using Prepared Statements (with PHP PDO)
<?php
// model.php
require_once "lib/database.php";
function get_all_posts()
{
$posts = db_get_all_posts();
return $posts;
}
function get_post_by_id($id)
{
$row = db_get_post_by_id($id);
return $row;
}
?>-----------------------------
<?php
// lib/database.php
require_once "../conf.php";
function db_connect()
{
$db = new PDO("$engine:host=$hostname;dbname=$dbname", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // depends on what ERRMODE to use...
// cf http://mx2.php.net/manual/en/pdo.setattribute.php
return $db;
}
function db_get_all_posts()
{
try {
$posts = array();
$db = db_connect();
$sql = "SELECT id, title FROM post";
$result = $db->query($sql);
foreach ($result as $row) {
$posts[] = $row;
}
$db = null; // close the database connection
return $posts;
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
function db_get_post_by_id($id)
{
try {
$db = db_connect();
$sql = "SELECT date, title, body FROM post WHERE id = :id";
// $sql = "SELECT date, title, body FROM post WHERE id = ?";
$stmt = $db->prepare($sql);
$stmt->execute(array(':id' => $id));
// $stmt->execute($id);
$row = $stmt->fetch();
// $stmt->execute(':id' => $otro_id);
return $row;
}
catch (PDOException $e) {
echo $e->getMessage();
}
}
?>----------------------
<?php
//../conf.php
$engine = "mysql";
$hostname = "localhost";
$username = "username";
$password = "passwordsecret";
$dbname = "blog";
Using POO
// without POO
//templates/show.php
<?php $title = $post['title'] ?>
<?php ob_start() ?>
<?php echo $post['title'] >
<?php echo $post['date'] >
<?php echo date('d-m-Y', $post['date']) >
<?php echo $post['body'] >
<?php $content = ob_get_clean() ?>
<?php include 'layout.php' ?>
--------------
// with POO
//templates/show.php
<?php $title = $post->title ?>
<?php ob_start() ?>
<?php echo $post->title ?>
<?php echo $post->date ?>
<?php echo $post->getDate('d-m-Y ') ?>
<?php echo $post->getDate() ?>
<?php echo $post->getDateInDMY() ?>
<?php echo $post->body ?>
-----------------
<?php
//Posts.php
class Posts
{
$id;
$title;
$date;
$body;
function getDate($format)
{
return date($format, $this->date);
}
function getDate()
{
return $this->getDate('d-m-Y');
}
function getDateInDMY()
{
return $this->getDate();
}
}