PHP Data Object (PDO)

An object in PHP to manage data

PDO

  • exec
  • prepare
  • query
  • lastInsertId

PDOStatement

  • execute
  • fetch
  • fetchAll
  • fetchColumn
  • fetchObject
  • rowCount

PDOException

  • getMessage (return string)

Some sample code:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass); //establish connection
$dbh = null; //close connection
 
//insert by prepare statement
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (?, ?)");
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $value);
 
// insert one row
$name = 'one';
$value = 1;
$stmt->execute();
 
//get result
$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name LIKE ?");
 
if ($stmt->execute(array("%$_GET[name]%"))) {
  while ($row = $stmt->fetch()) {
    print_r($row);
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *