<?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-update.php',
    1 => 'pg_update',
    2 => 'Update table',
  ),
  'up' => 
  array (
    0 => 'ref.pgsql.php',
    1 => 'PostgreSQL Functions',
  ),
  'prev' => 
  array (
    0 => 'function.pg-untrace.php',
    1 => 'pg_untrace',
  ),
  'next' => 
  array (
    0 => 'function.pg-version.php',
    1 => 'pg_version',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/pgsql/functions/pg-update.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.pg-update" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">pg_update</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.3.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">pg_update</span> &mdash; <span class="dc-title">
   Update table
  </span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.pg-update-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>pg_update</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<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>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$table_name</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$values</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$conditions</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$flags</code><span class="initializer"> = <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-exec">PGSQL_DML_EXEC</a></code></strong></span></span><br>): <span class="type"><span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>pg_update()</strong></span> updates records that matches
   <code class="parameter">conditions</code> with <code class="parameter">values</code>.
  </p>
  <p class="para">
   If <code class="parameter">flags</code> is specified,
   <span class="function"><a href="function.pg-convert.php" class="function">pg_convert()</a></span> is applied to
   <code class="parameter">values</code> with the specified flags.
  </p>
  <p class="para">
   By default <span class="function"><strong>pg_update()</strong></span> passes raw values.
   Values must be escaped or the <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-escape">PGSQL_DML_ESCAPE</a></code></strong> flag
   must be specified in <code class="parameter">flags</code>.
   <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-escape">PGSQL_DML_ESCAPE</a></code></strong> quotes and escapes parameters/identifiers.
   Therefore, table/column names become case sensitive.
  </p>
  <p class="para">
   Note that neither escape nor prepared query can protect LIKE query,
   JSON, Array, Regex, etc. These parameters should be handled
   according to their contexts. i.e. Escape/validate values.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.pg-update-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.</p>
     </dd>
    
    
     <dt><code class="parameter">table_name</code></dt>
     <dd>
      <p class="para">
       Name of the table into which to update rows.
      </p>
     </dd>
    
    
     <dt><code class="parameter">values</code></dt>
     <dd>
      <p class="para">
       An <span class="type"><a href="language.types.array.php" class="type array">array</a></span> whose keys are field names in the table <code class="parameter">table_name</code>,
       and whose values are what matched rows are to be updated to.
      </p>
     </dd>
    
    
     <dt><code class="parameter">conditions</code></dt>
     <dd>
      <p class="para">
       An <span class="type"><a href="language.types.array.php" class="type array">array</a></span> whose keys are field names in the table <code class="parameter">table_name</code>,
       and whose values are the conditions that a row must meet to be updated.
      </p>
     </dd>
    
    
     <dt><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       Any number of <strong><code><a href="pgsql.constants.php#constant.pgsql-conv-force-null">PGSQL_CONV_FORCE_NULL</a></code></strong>,
       <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-no-conv">PGSQL_DML_NO_CONV</a></code></strong>,
       <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-escape">PGSQL_DML_ESCAPE</a></code></strong>,
       <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-exec">PGSQL_DML_EXEC</a></code></strong>,
       <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-async">PGSQL_DML_ASYNC</a></code></strong> or
       <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-string">PGSQL_DML_STRING</a></code></strong> combined. If <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-string">PGSQL_DML_STRING</a></code></strong> is part of the
       <code class="parameter">flags</code> then query string is returned. When <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-no-conv">PGSQL_DML_NO_CONV</a></code></strong>
       or <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-escape">PGSQL_DML_ESCAPE</a></code></strong> is set, it does not call <span class="function"><a href="function.pg-convert.php" class="function">pg_convert()</a></span> internally.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.pg-update-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> on success or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on failure.  Returns <span class="type"><a href="language.types.string.php" class="type string">string</a></span> if <strong><code><a href="pgsql.constants.php#constant.pgsql-dml-string">PGSQL_DML_STRING</a></code></strong> is passed
   via <code class="parameter">flags</code>.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.pg-update-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>
  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-update-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>pg_update()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php <br />  $db </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'dbname=foo'</span><span style="color: #007700">);<br />  </span><span style="color: #0000BB">$data </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'field1'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'AA'</span><span style="color: #007700">, </span><span style="color: #DD0000">'field2'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'BB'</span><span style="color: #007700">);<br />  </span><span style="color: #FF8000">// This is safe somewhat, since all values are escaped.<br />  // However PostgreSQL supports JSON/Array. These are not<br />  // safe by neither escape nor prepared query.<br />  </span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_update</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">, </span><span style="color: #DD0000">'post_log'</span><span style="color: #007700">, </span><span style="color: #0000BB">$_POST</span><span style="color: #007700">, </span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br />  if (</span><span style="color: #0000BB">$res</span><span style="color: #007700">) {<br />      echo </span><span style="color: #DD0000">"Data is updated: </span><span style="color: #0000BB">$res</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />  } else {<br />      echo </span><span style="color: #DD0000">"User must have sent wrong inputs\n"</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-update-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.pg-convert.php" class="function" rel="rdfs-seeAlso">pg_convert()</a> - Convert associative array values into forms suitable for SQL statements</span></li>
   </ul>
  </p>
 </div>

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