PDO
PDO
PDO::__construct
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
PDO::beginTransaction
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
var_dump($dbh->beginTransaction());
PDO::commit
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
$dbh->beginTransaction();
$dbh->exec("UPDATE users SET name = 'Jerry' WHERE id = 1");
var_dump($dbh->commit());
PDO::errorCode
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
var_dump($dbh->errorCode());
PDO::errorInfo
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
var_dump($dbh->errorInfo());
PDO::exec
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
var_dump($dbh->exec("UPDATE users SET name = 'Jerry' WHERE id = 1"));
PDO::getAttribute
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
var_dump($dbh->getAttribute(PDO::ATTR_AUTOCOMMIT));
var_dump($dbh->getAttribute(PDO::ATTR_CASE));
var_dump($dbh->getAttribute(PDO::ATTR_CLIENT_VERSION));
PDO::getAvailableDrivers
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
var_dump(PDO::getAvailableDrivers());
var_dump($dbh->getAvailableDrivers());
PDO::inTransaction
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
var_dump($dbh->inTransaction());
$dbh->beginTransaction();
var_dump($dbh->inTransaction());
PDO::lastInsertId
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
var_dump($dbh->lastInsertId());
PDO::prepare
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
var_dump($dbh->prepare("SELECT * FROM users WHERE id = :id"));
var_dump($dbh->prepare("SELECT * FROM users WHERE id = ?"));
PDO::query
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
var_dump($dbh->query("SELECT * FROM users"));
foreach ($dbh->query("SELECT * FROM users") as $row) {
var_dump($row['id'], $row['name']);
}
PDO::quote
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
$string = 'foo';
var_dump($dbh->quote($string));
PDO::rollBack
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
$dbh->beginTransaction();
$dbh->exec("UPDATE users SET name = 'Jerry' WHERE id = 1");
var_dump($dbh->rollBack());
PDO::setAttribute
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
var_dump($dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER));
var_dump($dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER));
PDOStatement
PDOStatement::bindColumn
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
$sth = $dbh->prepare("SELECT * FROM users");
$sth->execute();
var_dump($sth->bindColumn('id', $id));
var_dump($sth->bindColumn('name', $name));
while ($sth->fetch()) {
var_dump(sprintf('#%d %s', $id, $name));
}
PDOStatement::bindParam
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
$sth = $dbh->prepare("SELECT * FROM users WHERE id = :id AND name = :name");
$id = 1;
$name = 'Tom';
var_dump($sth->bindParam(':id', $id, PDO::PARAM_INT));
var_dump($sth->bindParam(':name', $name, PDO::PARAM_STR));
$sth->execute();
var_dump($sth->fetchAll());
$sth = $dbh->prepare("SELECT * FROM users WHERE id = ? AND name = ?");
$id = 1;
$name = 'Tom';
var_dump($sth->bindParam(1, $id, PDO::PARAM_INT));
var_dump($sth->bindParam(2, $name, PDO::PARAM_STR));
$sth->execute();
var_dump($sth->fetchAll());
PDOStatement::bindValue
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
$sth = $dbh->prepare("SELECT * FROM users WHERE id = :id AND name = :name");
var_dump($sth->bindValue(':id', 1, PDO::PARAM_INT));
var_dump($sth->bindValue(':name', 'Tom', PDO::PARAM_STR));
$sth->execute();
var_dump($sth->fetchAll());
$sth = $dbh->prepare("SELECT * FROM users WHERE id = ? AND name = ?");
var_dump($sth->bindValue(1, 1, PDO::PARAM_INT));
var_dump($sth->bindValue(2, 'Tom', PDO::PARAM_STR));
$sth->execute();
var_dump($sth->fetchAll());
PDOStatement::closeCursor
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
$sth = $dbh->prepare("SELECT * FROM users");
$sth->execute();
$sth->fetch();
var_dump($sth->closeCursor());
PDOStatement::columnCount
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
$sth = $dbh->prepare("SELECT * FROM users");
$sth->execute();
var_dump($sth->columnCount());
PDOStatement::debugDumpParams
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
$sth = $dbh->prepare("SELECT * FROM users WHERE id = :id AND name = :name");
$sth->bindValue(':id', 1, PDO::PARAM_INT);
$sth->bindValue(':name', 'Tom', PDO::PARAM_STR);
$sth->execute();
var_dump($sth->debugDumpParams());
PDOStatement::errorCode
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
$sth = $dbh->prepare("SELECT * FROM users");
$sth->execute();
var_dump($sth->errorCode());
PDOStatement::errorInfo
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
$sth = $dbh->prepare("SELECT * FROM users");
$sth->execute();
var_dump($sth->errorInfo());
PDOStatement::execute
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
$sth = $dbh->prepare("SELECT * FROM users WHERE id = :id AND name = :name");
$sth->bindValue(':id', 1, PDO::PARAM_INT);
$sth->bindValue(':name', 'Tom', PDO::PARAM_STR);
var_dump($sth->execute());
var_dump($sth->fetchAll());
$sth = $dbh->prepare("SELECT * FROM users WHERE id = :id AND name = :name");
var_dump($sth->execute([':id' => 1, ':name' => 'Tom']));
var_dump($sth->fetchAll());
$sth = $dbh->prepare("SELECT * FROM users WHERE id = ? AND name = ?");
$sth->bindValue(1, 1, PDO::PARAM_INT);
$sth->bindValue(2, 'Tom', PDO::PARAM_STR);
var_dump($sth->execute());
var_dump($sth->fetchAll());
$sth = $dbh->prepare("SELECT * FROM users WHERE id = ? AND name = ?");
var_dump($sth->execute([1, 'Tom']));
var_dump($sth->fetchAll());
PDOStatement::fetch
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
$sth = $dbh->prepare("SELECT * FROM users");
$sth->execute();
var_dump($sth->fetch());
$sth->execute();
var_dump($sth->fetch(PDO::FETCH_ASSOC));
$sth->execute();
var_dump($sth->fetch(PDO::FETCH_OBJ));
PDOStatement::fetchAll
<?php
class Foo
{
}
function foo(int $id, string $name): string
{
return sprintf('#%d %s', $id, $name);
}
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
$sth = $dbh->prepare("SELECT * FROM users");
$sth->execute();
var_dump($sth->fetchAll());
$sth->execute();
var_dump($sth->fetchAll(PDO::FETCH_ASSOC));
$sth->execute();
var_dump($sth->fetchAll(PDO::FETCH_COLUMN, 1));
$sth->execute();
var_dump($sth->fetchAll(PDO::FETCH_COLUMN | PDO::FETCH_GROUP));
$sth->execute();
var_dump($sth->fetchAll(PDO::FETCH_CLASS, 'Foo'));
$sth->execute();
var_dump($sth->fetchAll(PDO::FETCH_FUNC, 'foo'));
PDOStatement::fetchColumn
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
$sth = $dbh->prepare("SELECT * FROM users");
$sth->execute();
var_dump($sth->fetchColumn());
var_dump($sth->fetchColumn(1));
PDOStatement::fetchObject
<?php
class Foo
{
}
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
$sth = $dbh->prepare("SELECT * FROM users");
$sth->execute();
var_dump($sth->fetchObject());
var_dump($sth->fetchObject('Foo'));
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
$sth = $dbh->prepare("SELECT * FROM users");
$sth->execute();
var_dump($sth->getColumnMeta(0));
var_dump($sth->getColumnMeta(1));
PDOStatement::nextRowset
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
$sth = $dbh->prepare("SELECT * FROM users");
$sth->execute();
var_dump($sth->fetch());
var_dump($sth->nextRowset());
var_dump($sth->fetch());
PDOStatement::rowCount
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
$sth = $dbh->prepare("UPDATE users SET name = :name WHERE id = :id");
$sth->bindValue(':name', 'Jerry', PDO::PARAM_STR);
$sth->bindValue(':id', 1, PDO::PARAM_INT);
$sth->execute();
var_dump($sth->rowCount());
PDOStatement::setFetchMode
<?php
try {
$dbh = new PDO('mysql:host=127.0.0.1;port=3306;dbname=test', 'root', 'root');
} catch (PDOException $e) {
echo $e->getMessage();
}
$sth = $dbh->prepare("SELECT * FROM users");
$sth->execute();
var_dump($sth->setFetchMode(PDO::FETCH_ASSOC));
var_dump($sth->fetchAll());