<!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>Executes a query against a given database and returns a result handle</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.sqlite-prev.html">sqlite_prev</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.sqlite-rewind.html">sqlite_rewind</a></div> <div class="up"><a href="ref.sqlite.html">SQLite Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.sqlite-query" class="refentry"> <div class="refnamediv"> <h1 class="refname">sqlite_query</h1> <h1 class="refname">SQLiteDatabase::query</h1> <p class="verinfo">(PHP 5 < 5.4.0, PECL sqlite >= 1.0.0)</p><p class="refpurpose"><span class="refname">sqlite_query</span> -- <span class="refname">SQLiteDatabase::query</span> — <span class="dc-title">Executes a query against a given database and returns a result handle</span></p> </div> <div class="refsect1 description" id="refsect1-function.sqlite-query-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">resource</span> <span class="methodname"><strong>sqlite_query</strong></span> ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$dbhandle</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$query</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$result_type</code><span class="initializer"> = SQLITE_BOTH</span></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$error_msg</code></span> ]] )</div> <div class="methodsynopsis dc-description"> <span class="type">resource</span> <span class="methodname"><strong>sqlite_query</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$query</code></span> , <span class="methodparam"><span class="type">resource</span> <code class="parameter">$dbhandle</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$result_type</code><span class="initializer"> = SQLITE_BOTH</span></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$error_msg</code></span> ]] )</div> <p class="para rdfs-comment">Object oriented style (method):</p> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type"><span class="type SQLiteResult">SQLiteResult</span></span> <span class="methodname"><strong>SQLiteDatabase::query</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$query</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$result_type</code><span class="initializer"> = SQLITE_BOTH</span></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$error_msg</code></span> ]] )</div> <p class="para rdfs-comment"> Executes an SQL statement given by the <em><code class="parameter">query</code></em> against a given database handle. </p> </div> <div class="refsect1 parameters" id="refsect1-function.sqlite-query-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">dbhandle</code></em></span> <dd> <p class="para"> The SQLite Database resource; returned from <span class="function"><a href="function.sqlite-open.html" class="function">sqlite_open()</a></span> when used procedurally. This parameter is not required when using the object-oriented method. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">query</code></em></span> <dd> <p class="para"> The query to be executed. </p> <p class="para"> Data inside the query should be <a href="function.sqlite-escape-string.html" class="link">properly escaped</a>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">result_type</code></em></span> <dd> <p class="para">The optional <em><code class="parameter">result_type</code></em> parameter accepts a constant and determines how the returned array will be indexed. Using <strong><code>SQLITE_ASSOC</code></strong> will return only associative indices (named fields) while <strong><code>SQLITE_NUM</code></strong> will return only numerical indices (ordinal field numbers). <strong><code>SQLITE_BOTH</code></strong> will return both associative and numerical indices. <strong><code>SQLITE_BOTH</code></strong> is the default for this function.</p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">error_msg</code></em></span> <dd> <p class="para"> The specified variable will be filled if an error occurs. This is specially important because SQL syntax errors can't be fetched using the <span class="function"><a href="function.sqlite-last-error.html" class="function">sqlite_last_error()</a></span> function. </p> </dd> </dt> </dl> </p> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">Two alternative syntaxes are supported for compatibility with other database extensions (such as MySQL). The preferred form is the first, where the <em><code class="parameter">dbhandle</code></em> parameter is the first parameter to the function.</span></p></blockquote> </div> <div class="refsect1 returnvalues" id="refsect1-function.sqlite-query-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> This function will return a result handle or <strong><code>FALSE</code></strong> on failure. For queries that return rows, the result handle can then be used with functions such as <span class="function"><a href="function.sqlite-fetch-array.html" class="function">sqlite_fetch_array()</a></span> and <span class="function"><a href="function.sqlite-seek.html" class="function">sqlite_seek()</a></span>. </p> <p class="para"> Regardless of the query type, this function will return <strong><code>FALSE</code></strong> if the query failed. </p> <p class="para"> <span class="function"><strong>sqlite_query()</strong></span> returns a buffered, seekable result handle. This is useful for reasonably small queries where you need to be able to randomly access the rows. Buffered result handles will allocate memory to hold the entire result and will not return until it has been fetched. If you only need sequential access to the data, it is recommended that you use the much higher performance <span class="function"><a href="function.sqlite-unbuffered-query.html" class="function">sqlite_unbuffered_query()</a></span> instead. </p> </div> <div class="refsect1 changelog" id="refsect1-function.sqlite-query-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.1.0</td> <td> Added the <em><code class="parameter">error_msg</code></em> parameter </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 notes" id="refsect1-function.sqlite-query-notes"> <h3 class="title">Notes</h3> <div class="warning"><strong class="warning">Warning</strong> <p class="simpara"> SQLite <em class="emphasis">will</em> execute multiple queries separated by semicolons, so you can use it to execute a batch of SQL that you have loaded from a file or have embedded in a script. However, this works only when the result of the function is not used - if it is used, only the first SQL statement would be executed. Function <span class="function"><a href="function.sqlite-exec.html" class="function">sqlite_exec()</a></span> will always execute multiple SQL statements. </p> <p class="simpara"> When executing multiple queries, the return value of this function will be <strong><code>FALSE</code></strong> if there was an error, but undefined otherwise (it might be <strong><code>TRUE</code></strong> for success or it might return a result handle). </p> </div> </div> <div class="refsect1 seealso" id="refsect1-function.sqlite-query-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.sqlite-unbuffered-query.html" class="function" rel="rdfs-seeAlso">sqlite_unbuffered_query()</a> - Execute a query that does not prefetch and buffer all data</span></li> <li class="member"> <span class="function"><a href="function.sqlite-array-query.html" class="function" rel="rdfs-seeAlso">sqlite_array_query()</a> - Execute a query against a given database and returns an array</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.sqlite-prev.html">sqlite_prev</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.sqlite-rewind.html">sqlite_rewind</a></div> <div class="up"><a href="ref.sqlite.html">SQLite Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>