Difference between revisions of "Object Oriented PHP"
Jump to navigation
Jump to search
(Created page with "==Connecting to the database == <pre> try {$db = new PDO('mysqli:host=localhost;dbname=name;','username','passowrd',); } catch(PDOException $e){ die("database connecti...") |
() |
||
Line 31: | Line 31: | ||
</pre> | </pre> | ||
---- | ---- | ||
[[#Connecting to the database|Back To Top]]-[[Main_Page| Home]] - [[PHP|Category]] | [[#Connecting to the database|Back To Top]]-[[Main_Page| Home]] - [[PHP|Category]] |
Latest revision as of 16:01, 17 August 2016
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