PHPEveryday.com PHP and Web Development Tutorial
What are you looking for?


Table of Content
PHP Data Object Tutorial

PDO Intro
PDO Activation
PDO Connection
PDO Portable Conn
PDO Fetch Mode
PDO Error Hand.
PDO Statement
PDO Placeholders
PDO Insert & Update
PDO Bound Values
PDO BLOBs
PDO: Alt. BLOB
PDO Conn Attributes
PDO Error Mode
PDO Persistent
PDO Attributes

PDO: Error Handling


PHP Data Objects (PDO) Step By Step Tutorial - Part 6: When everything is okey, we didn't need error checking. But, we never know (especially at real world) what will happen, error handling is needed. We should check for errors when opening the connection to database, select database, and run query.

We can implement error handling at PDO like this:

<?php
// configuration
$dbtype		= "sqlite";
$dbhost 	= "localhost";
$dbname		= "test";
$dbuser		= "root";
$dbpass		= "admin";

try{
  // database connection
  $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
}
catch(PDOException $pe)
{
  die('Connection error, because: ' .$pe->getMessage());
}


// query
$sql = "SELECT title FROM books ORDER BY title";
$q	 = $conn->query($sql);

if(!$q)
{
  die("Execute query error, because: ". $conn->errorInfo());
}

$q->setFetchMode(PDO::FETCH_BOTH);

// fetch
while($r = $q->fetch()){
  print_r($r);
}


?>

Previous: PDO: Posibble Fetch Mode
Next: PDO: Prepared Statement



Series this article:
PDO: Introduction PHP Data Object
PDO: Activation PHP Data Objects Extension
PDO: Connecting Use PHP Data Object
PDO: Portable Connection to Database
PDO: Posibble Fetch Mode
PDO: Error Handling
PDO: Prepared Statement
PDO: Positional and Named Placeholders
PDO: Insert and Update Statement Use Prepared Statement
PDO: Prepared Statement and Bound Values
PDO: Working With BLOBs
PDO: Alternative Retrieve BLOB Data
PDO: Setting Connection Attributes
PDO: Error Mode Attributes
PDO: Improve Performance with Persistent Connection
PDO: Getting Connection Attributes


Tag: Category: PHP Database Post : April 19th 2008 Read: 4,548 Bookmark and Share

blog comments powered by Disqus


Database Tutorial
  • Learn PHP MySQL
  • Learn PHP ADOdb
  • Learn PHP Data Object/PDO
  • Learn PHP XML
  • Learn PHP SimpleXML
Security Tutorial
  • Learn PHP Security
  • Learn HTTP Authentication
  • Learn PHPSecureSite
Framework Tutorial
  • Learn CodeIgniter
  • Learn Joomla
  • Learn Smarty
  • Learn Zend Framework
Template Tutorial
  • Learn Joomla Template
  • Learn WordPress Template
API Tutorial
  • Learn Facebook
JS Framework Tutorial
  • Learn MooTools
  • Learn JQuery
AJAX Tutorial
  • Learn AJAX in 10 Minutes
  • Learn AJAX Client Side
  • Learn AJAX PHP
  • Learn AJAX Remote Server
  • Learn AJAX Repetitive
  • Learn AJAX MySQL
  • Learn AJAX Grid
Web Services Tutorial
  • Learn Web Services NuSOAP
  • Learn Web Services WSDL
  • Learn Web Services WSDL Array
  • Learn Web Services .NET Grid
  • Learn Web Services WDDX
Package Post
  • Joomla Intro
  • Joomla Component
  • Joomla Module
  • Joomla MVC
  • Joomla MVC Backend
  • PostNuke Intro
  • Zend Framework Intro
  • Zend Framework Action
  • Zend Framework Database
  • Zend Framework Registry
  • Zend Framework Config
  • Zend Framework Login
  • Zend Framework Session
  • PHP Array Tips
  • PHP File Tips
  • PHP Email
  • PHP Ms Excel
  • PHP Pattern
  • PHP SMS
  • Flash Database
  • PHP Multitier
  • jQuery Introduction
  • jQuery Selectors
  • Portable Web Server
  • Web Mobile Intro
  • Drupal Installation
  • Drupal Configuration