3 ext/pq provides three means to execute queries against the PostgreSQL server:
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.
13 ## Simple plain execution of a query
15 pq\Connection::exec() accepts a single argument, a ***query string***, containing a single or multiple SQL queries, separated by semi-colon.
18 $result = $c->exec("SELECT 1*s,2*s,3*s FROM generate_series(1,3) s");
21 An object of class pq\Result is returned on success. See [Fetching results](pq/Result/: Fetching Results) for details.
24 > Only the last result will be returned, if the query string contains more than one SQL query.
27 ## Automatic prepare & execute of an unnamed statement
29 pq\Connection::execParams() accepts a ***query string*** with a single SQL query as first argument, which will be prepared as an unnamed statement.
31 The second argument is an ***array of parameters*** to execute the prepared statement with.
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.
36 $result = $c->execParams("SELECT int($1) * s, int($2) * s, int($3) * s FROM generate_series(1,3) s", array(1,2,3));
39 An object of class pq\Result is returned on success. See [Fetching results](pq/Result/: Fetching Results) for details.
42 ## Explicit prepare & execute of a named statement
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.
46 The second argument is a ***query string*** containing a single SQL query, which will be prepared on the server.
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.
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));
57 An object of class pq\Statement will be returned on success. See [Prepared statements](pq/: Prepared Statements) for details.