Dan's Website

Ramblings of a Disaffected Geek

Home > Software > Using PDO with SQLlite or Mysql
Using PDO with SQLlite or Mysql

Example code for using PDO and exception handling.

I just thought I'd try it with SQLite and it's very simple.  Uncommenting the second constructor shows how it runs with MySQL.

 

<?php
	$db = new PDO("sqlite:/Users/dmakovec/test.sqlite");
	//$db = new PDO("mysql:host=localhost;dbname=test","root","");
	$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 
	try {
		foreach($db->query("SELECT * from test") as $row) {
			print "{$row['id']} : {$row['name']}\n";
		}
		
	} catch (PDOException $e) {
		print <<<EOF
		An Exception occurred:
		File:    {$e->getFile()}
		Line:    {$e->getLine()}
		Code:    {$e->getCode()}
		Message: {$e->getMessage()}
		Trace:   
{$e->getTraceAsString()}
EOF;
	}

Comments
Add New Search
Write comment
Name:
Email:
 
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Please input the anti-spam code that you can read in the image.

3.23 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."