PHP Data Objects

PHP Data Objects (PDO)

Connecting to a DBMS

Example: Connect to a SQLite Database

// connect to the SQLite database
// named example.db
$dsn = "sqlite:example.db";
$db = new PDO($dsn);

// do some stuff with the database

// close the database connection
$db = null;

The PDO::query Function

PDO::query Example

// connect to example.db
$db = new PDO("sqlite:example.db");

// make a query
$sql = "SELECT * FROM table_name";

// execute the query with PDO::query
$stmt = $db->query($sql);

Retrieve Data from a PDOStatement

Options for PDOStatement::fetch

The PDO::exec Function

PDO Prepared Statements

The PDO::prepare Function

The PDOStatement::execute Function

PDO Prepared Statement Examples