Object Oriented PHP
Jump to navigation
Jump to search
Connecting to the database
try {$db = new PDO('mysqli:host=localhost;dbname=name;','username','passowrd',); } catch(PDOException $e){ die("database connection failed"); } ******* Get available drivers on server $drivers = PDO::getAvailableDrivers(); var_dump($drivers); ********* make a query $user = $db -> query("SELECT * FORM table WHERE id=1"); $user ->fetch(PDO::FETCH_ASSOC); echo $user['username'];
DSNs Connecting to the database
<?php $dsn = 'mysql:host=localhost;dbname=oophp'; $dsn = 'mysql:host=localhost;dbname=oophp;port=8889'; $dsn = 'sqlite:C:/xampp/htdocs/oophp/sqlite/oophp.db'; // path to database $dsn = 'sqlite:/Applications/MAMP/htdocs/oophp/sqlite/oophp.db'; $db = new PDO($dsn, 'unsername', 'password'); // for connecting to MYSQL $db = new PDO($dsn) // for connecting to sqlite