mdb)};Dbq=C:\\db.mdb;Uid=Admin
pgsql:dbname=example;user=nobody;password=change_me;host=localhost;
port=5432
sqlite:/opt/databases/mydb.sq3
sqlite::memory:
sqlite2:/opt/databases/mydb.sq2
sqlite2::memory:
Using Prepared Statements with PDO
Using PDO you can run prepared statements against your database. The benefits are
the same as before. It increases the performance for multiple calls by parsing and
caching the server-side query and it also eliminates the chance of SQL injection.
PDO prepared statements can take named variables, unlike what we've seen in the
examples of MySQLi.
Let's take a look at the following example to understand this:
$dsn = 'mysql:dbname=test;host=localhost;';
$user = 'username';
$password = 'password';
try {
$pdo = new PDO($dsn, $user, $password);
Database in an OOP Way
[ 180 ]
} catch (PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
}
$stmt = $pdo->prepare("select id from users where name=:name");
$name = "tipu";
$stmt->bindParam(":name",$name, PDO::PARAM_STR);
$stmt->execute();
$stmt->bindColumn("id",$id);
$stmt->fetch();
echo $id;
?>
But you can also run the example like this:
$dsn = 'mysql:dbname=test;host=localhost;';
$user = 'username';
$password = 'password';
try {
$pdo = new PDO($dsn, $user, $password);
}
catch (PDOException $e)
{
echo 'Connection failed: ' .
Pages:
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194