<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.pgsql.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.pg-query-params.php',
    1 => 'pg_query_params',
    2 => 'Submits a command to the server and waits for the result, with the ability to pass parameters separately from the SQL command text',
  ),
  'up' => 
  array (
    0 => 'ref.pgsql.php',
    1 => 'PostgreSQL Functions',
  ),
  'prev' => 
  array (
    0 => 'function.pg-query.php',
    1 => 'pg_query',
  ),
  'next' => 
  array (
    0 => 'function.pg-result-error.php',
    1 => 'pg_result_error',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/pgsql/functions/pg-query-params.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.pg-query-params" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">pg_query_params</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">pg_query_params</span> &mdash; <span class="dc-title">Submits a command to the server and waits for the result, with the ability to pass parameters separately from the SQL command text</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.pg-query-params-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>pg_query_params</strong></span>(<span class="methodparam"><span class="type"><a href="class.pgsql-connection.php" class="type PgSql\Connection">PgSql\Connection</a></span> <code class="parameter">$connection</code><span class="initializer"> = ?</span></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$query</code></span>, <span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$params</code></span>): <span class="type"><span class="type"><a href="class.pgsql-result.php" class="type PgSql\Result">PgSql\Result</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
    Submits a command to the server and waits for the result, with the ability 
    to pass parameters separately from the SQL command text.
  </p>
  <p class="para">
    <span class="function"><strong>pg_query_params()</strong></span> is like <span class="function"><a href="function.pg-query.php" class="function">pg_query()</a></span>, 
    but offers additional functionality: parameter 
    values can be specified separately from the command string proper. 
  </p>
  <p class="para">
    If parameters are used, they are referred to in the
    <code class="parameter">query</code> string as $1, $2, etc. The same parameter may
    appear more than once in the <code class="parameter">query</code>; the same value
    will be used in that case. <code class="parameter">params</code> specifies the
    actual values of the parameters. A <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> value in this array means the
    corresponding parameter is SQL <code class="literal">NULL</code>.
  </p>
  <p class="para">
    The primary advantage of <span class="function"><strong>pg_query_params()</strong></span> over <span class="function"><a href="function.pg-query.php" class="function">pg_query()</a></span> 
    is that parameter values 
    may be separated from the <code class="parameter">query</code> string, thus avoiding the need for tedious 
    and error-prone quoting and escaping. Unlike <span class="function"><a href="function.pg-query.php" class="function">pg_query()</a></span>, 
    <span class="function"><strong>pg_query_params()</strong></span> allows at 
    most one SQL command in the given string. (There can be semicolons in it, 
    but not more than one nonempty command.)
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.pg-query-params-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">connection</code></dt>
     <dd>
      <p class="para">An <span class="classname"><a href="class.pgsql-connection.php" class="classname">PgSql\Connection</a></span> instance.
When <code class="parameter">connection</code> is unspecified, the default connection is used.
The default connection is the last connection made by <span class="function"><a href="function.pg-connect.php" class="function">pg_connect()</a></span>
or <span class="function"><a href="function.pg-pconnect.php" class="function">pg_pconnect()</a></span>.
<div class="warning"><strong class="warning">Warning</strong><p class="simpara">As of PHP 8.1.0, using the default connection is deprecated.</p></div></p>
     </dd>
    
    
     <dt><code class="parameter">query</code></dt>
     <dd>
      <p class="para">
       The parameterized SQL statement.  Must contain only a single statement.
       (multiple statements separated by semi-colons are not allowed.)  If any parameters 
       are used, they are referred to as $1, $2, etc.
      </p>
      <p class="para">
       User-supplied values should always be passed as parameters, not
       interpolated into the query string, where they form possible
       <a href="security.database.sql-injection.php" class="link"> SQL injection</a>
       attack vectors and introduce bugs when handling data containing quotes.
       If for some reason you cannot use a parameter, ensure that interpolated
       values are <a href="function.pg-escape-string.php" class="link">properly escaped</a>.
      </p>
     </dd>
    
    
     <dt><code class="parameter">params</code></dt>
     <dd>
      <p class="para">
        An array of parameter values to substitute for the $1, $2, etc. placeholders
        in the original prepared query string.  The number of elements in the array
        must match the number of placeholders.
      </p>
      <p class="para">
       Values intended for <code class="literal">bytea</code> fields are not supported as
       parameters. Use <span class="function"><a href="function.pg-escape-bytea.php" class="function">pg_escape_bytea()</a></span> instead, or use the
       large object functions.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.pg-query-params-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
    An <span class="classname"><a href="class.pgsql-result.php" class="classname">PgSql\Result</a></span> instance on success,  or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on failure.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.pg-query-params-changelog">
  <h3 class="title">Changelog</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
 <td>8.1.0</td>
 <td>
  Returns an <span class="classname"><a href="class.pgsql-result.php" class="classname">PgSql\Result</a></span> instance now;
  previously, a <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> was returned.
 </td>
</tr>

     <tr>
 <td>8.1.0</td>
 <td>
  The <code class="parameter">connection</code> parameter expects an <span class="classname"><a href="class.pgsql-connection.php" class="classname">PgSql\Connection</a></span>
  instance now; previously, a <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> was expected.
 </td>
</tr>

    </tbody>
   
  </table>

 </div>

 
 <div class="refsect1 examples" id="refsect1-function.pg-query-params-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 Using <span class="function"><strong>pg_query_params()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Connect to a database named "mary"<br /></span><span style="color: #0000BB">$dbconn </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"dbname=mary"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Find all shops named Joe's Widgets.  Note that it is not necessary to<br />// escape "Joe's Widgets"<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_query_params</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbconn</span><span style="color: #007700">, </span><span style="color: #DD0000">'SELECT * FROM shops WHERE name = $1'</span><span style="color: #007700">, array(</span><span style="color: #DD0000">"Joe's Widgets"</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Compare against just using pg_query<br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_escape_string</span><span style="color: #007700">(</span><span style="color: #DD0000">"Joe's Widgets"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbconn</span><span style="color: #007700">, </span><span style="color: #DD0000">"SELECT * FROM shops WHERE name = '</span><span style="color: #007700">{</span><span style="color: #0000BB">$str</span><span style="color: #007700">}</span><span style="color: #DD0000">'"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.pg-query-params-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.pg-query.php" class="function" rel="rdfs-seeAlso">pg_query()</a> - Execute a query</span></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>