missing bits
[mdref/mdref-pq] / pq / Connection / : Executing Queries.md
1 # Executing queries
2
3 ext/pq provides three means to execute queries against the PostgreSQL server:
4
5 * pq\Connection::exec()
6 Simple plain execution of a query.
7 * pq\Connection::execParams()
8 Automatic prepare & execute of an unnamed statement.
9 * pq\Connection::prepare() and pq\Statement::exec()
10 Explicit prepare & execute of an named statement.
11
12
13 ## Simple plain execution of a query
14
15 pq\Connection::exec() accepts a single argument, a ***query string***, containing a single or multiple SQL queries, separated by semi-colon.
16
17 <?php
18 $result = $c->exec("SELECT 1*s,2*s,3*s FROM generate_series(1,3) s");
19 ?>
20
21 An object of class pq\Result is returned on success. See [Fetching results](pq/Result/: Fetching Results) for details.
22
23 > ***NOTE:***
24 > Only the last result will be returned, if the query string contains more than one SQL query.
25
26
27 ## Automatic prepare & execute of an unnamed statement
28
29 pq\Connection::execParams() accepts a ***query string*** with a single SQL query as first argument, which will be prepared as an unnamed statement.
30
31 The second argument is an ***array of parameters*** to execute the prepared statement with.
32
33 If the third argument is present, an ***array with pg_type OIDs***, those types will be used for the parameters. See [Using types](pq/: Using Types) for details.
34
35 <?php
36 $result = $c->execParams("SELECT int($1) * s, int($2) * s, int($3) * s FROM generate_series(1,3) s", array(1,2,3));
37 ?>
38
39 An object of class pq\Result is returned on success. See [Fetching results](pq/Result/: Fetching Results) for details.
40
41
42 ## Explicit prepare & execute of a named statement
43
44 pq\Connection::prepare() requires the ***statement name*** as string as first argument. This name is used later to refer to this prepared statement. See [Prepared statements](pq/: Prepared Statements) for details.
45
46 The second argument is a ***query string*** containing a single SQL query, which will be prepared on the server.
47
48 If the third argument is present, an ***array with pg_type OIDs***, those types will be used for the parameters. See [Using types](pq/: Using Types) for details.
49
50 <?php
51
52 $statement = $c->prepare("my_stm", "SELECT \$1::int*s,\$2::int*s,\$3::int*s FROM generate_series(1,3) s");
53 $result = $statement->exec(array(1,2,3));
54
55 ?>
56
57 An object of class pq\Statement will be returned on success. See [Prepared statements](pq/: Prepared Statements) for details.