pq/Cursor docs
[mdref/mdref-pq] / pq / Connection / prepare.md
1 # pq\Statement pq\Connection::prepare(string $name, string $query[, array $types = NULL])
2
3 Prepare a named statement for later execution with pq\Statement::execute().
4
5 ## Params:
6
7 * string $name
8 The identifying name of the prepared statement.
9 * string $query
10 The query to prepare.
11 * Optional array $types = NULL
12 An array of type OIDs for the substitution parameters.
13
14 ## Returns:
15
16 * pq\Statement, a prepared statement instance.
17
18 ## Throws:
19
20 * pq\Exception\InvalidArgumentException
21 * pq\Exception\BadMethodCallException
22 * pq\Exception\RuntimeException
23
24
25 ## Example:
26
27 <?php
28
29 $connection = new pq\Connection;
30
31 $statement = $connection->prepare(
32 "example",
33 "SELECT a from generate_series(1,9) a WHERE a > \$1",
34 [pq\Types::INT4]);
35 $result = $statement->exec([5]);
36
37 var_dump($result->fetchAllCols(0));
38
39 ?>
40
41 Yields:
42
43 array(4) {
44 [0]=>
45 int(6)
46 [1]=>
47 int(7)
48 [2]=>
49 int(8)
50 [3]=>
51 int(9)
52 }
53