<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Convert associative array values into forms suitable for SQL statements</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.pg-consume-input.html">pg_consume_input</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.pg-copy-from.html">pg_copy_from</a></div> <div class="up"><a href="ref.pgsql.html">PostgreSQL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.pg-convert" class="refentry"> <div class="refnamediv"> <h1 class="refname">pg_convert</h1> <p class="verinfo">(PHP 4 >= 4.3.0, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">pg_convert</span> — <span class="dc-title"> Convert associative array values into forms suitable for SQL statements </span></p> </div> <div class="refsect1 description" id="refsect1-function.pg-convert-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>pg_convert</strong></span> ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$connection</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$table_name</code></span> , <span class="methodparam"><span class="type">array</span> <code class="parameter">$assoc_array</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$options</code><span class="initializer"> = 0</span></span> ] )</div> <p class="para rdfs-comment"> <span class="function"><strong>pg_convert()</strong></span> checks and converts the values in <code class="parameter">assoc_array</code> into suitable values for use in an SQL statement. Precondition for <span class="function"><strong>pg_convert()</strong></span> is the existence of a table <code class="parameter">table_name</code> which has at least as many columns as <code class="parameter">assoc_array</code> has elements. The fieldnames in <code class="parameter">table_name</code> must match the indices in <code class="parameter">assoc_array</code> and the corresponding datatypes must be compatible. Returns an array with the converted values on success, <strong><code>FALSE</code></strong> otherwise. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Since PHP 5.6.0, it accepts boolean values, converting them to PostgreSQL booleans. String representations of boolean values are also supported. <strong><code>NULL</code></strong> is converted to PostgreSQL NULL. </p> <p class="para"> Prior to PHP 5.6.0, if there are boolean fields in <code class="parameter">table_name</code> don't use the constant <strong><code>TRUE</code></strong> in <code class="parameter">assoc_array</code>. It will be converted to the string 'TRUE' which is not a valid entry for boolean fields in PostgreSQL. Use one of "t", "true", 1, "y", "yes" instead. </p> </p></blockquote> </div> <div class="refsect1 parameters" id="refsect1-function.pg-convert-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">connection</code></dt> <dd> <p class="para"> PostgreSQL database connection resource. </p> </dd> <dt> <code class="parameter">table_name</code></dt> <dd> <p class="para"> Name of the table against which to convert types. </p> </dd> <dt> <code class="parameter">assoc_array</code></dt> <dd> <p class="para"> Data to be converted. </p> </dd> <dt> <code class="parameter">options</code></dt> <dd> <p class="para"> Any number of <strong><code>PGSQL_CONV_IGNORE_DEFAULT</code></strong>, <strong><code>PGSQL_CONV_FORCE_NULL</code></strong> or <strong><code>PGSQL_CONV_IGNORE_NOT_NULL</code></strong>, combined. </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.pg-convert-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> An <span class="type"><a href="language.types.array.html" class="type array">array</a></span> of converted values, or <strong><code>FALSE</code></strong> on error. </p> </div> <div class="refsect1 examples" id="refsect1-function.pg-convert-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2532"> <p><strong>Example #1 <span class="function"><strong>pg_convert()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php <br /> $dbconn </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 /> <br /> </span><span style="color: #0000BB">$tmp </span><span style="color: #007700">= array(<br /> </span><span style="color: #DD0000">'author' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'Joe Thackery'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'year' </span><span style="color: #007700">=> </span><span style="color: #0000BB">2005</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'title' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'My Life, by Joe Thackery'<br /> </span><span style="color: #007700">);<br /> <br /> </span><span style="color: #0000BB">$vals </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_convert</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbconn</span><span style="color: #007700">, </span><span style="color: #DD0000">'authors'</span><span style="color: #007700">, </span><span style="color: #0000BB">$tmp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 changelog" id="refsect1-function.pg-convert-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.6.0</td> <td> No longer experimental. Boolean/NULL data types are supported. Unknown/unsupported data types are escaped without validation. <span class="function"><strong>pg_convert()</strong></span> can be used with any data types. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 seealso" id="refsect1-function.pg-convert-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><span class="function"><a href="function.pg-meta-data.html" class="function" rel="rdfs-seeAlso">pg_meta_data()</a> - Get meta data for table</span></li> <li class="member"><span class="function"><a href="function.pg-insert.html" class="function" rel="rdfs-seeAlso">pg_insert()</a> - Insert array into table</span></li> <li class="member"><span class="function"><a href="function.pg-select.html" class="function" rel="rdfs-seeAlso">pg_select()</a> - Select records</span></li> <li class="member"><span class="function"><a href="function.pg-update.html" class="function" rel="rdfs-seeAlso">pg_update()</a> - Update table</span></li> <li class="member"><span class="function"><a href="function.pg-delete.html" class="function" rel="rdfs-seeAlso">pg_delete()</a> - Deletes records</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.pg-consume-input.html">pg_consume_input</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.pg-copy-from.html">pg_copy_from</a></div> <div class="up"><a href="ref.pgsql.html">PostgreSQL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>