Connections are established by creating instances of the PDO base class. It doesn't matter which driver you want to use; you always use the PDO class name. The constructor accepts parameters for specifying the database source (known as the DSN) and optionally for the username and password (if any).
Syntax:
$varname=new PDO($dsn, $username, $passwd, $options)
Example:
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test',$user,$pass);
?>
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test',$user,$pass);
?>
Example for closing connection
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);// use the connection here
// and now we're done; close it$dbh = null;?>
0 comments:
Post a Comment