CodeIgniter4/database/queries.html
2022-09-30 21:09:08 +00:00

789 lines
57 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Queries &mdash; CodeIgniter 4.2.6 documentation</title>
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
<link rel="shortcut icon" href="../_static/favicon.ico"/>
<!--[if lt IE 9]>
<script src="../_static/js/html5shiv.min.js"></script>
<![endif]-->
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/js/citheme.js"></script>
<script src="../_static/js/carbon.js"></script>
<script src="../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Generating Query Results" href="results.html" />
<link rel="prev" title="Connecting to your Database" href="connecting.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" style="background: #DD4814" >
<a href="../index.html">
<img src="../_static/ci-logo-text.png" class="logo" alt="Logo"/>
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a></li>
<li class="toctree-l2"><a class="reference internal" href="../intro/requirements.html">Server Requirements</a></li>
<li class="toctree-l2"><a class="reference internal" href="../intro/credits.html">Credits</a></li>
<li class="toctree-l2"><a class="reference internal" href="../intro/psr.html">PSR Compliance</a></li>
<li class="toctree-l2"><a class="reference internal" href="../license.html">License Agreement</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_composer.html">Composer Installation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_manual.html">Manual Installation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/running.html">Running Your App</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">Troubleshooting</a></li>
<li class="toctree-l2"><a class="reference internal" href="../changelogs/index.html">Change Logs</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">Upgrading From a Previous Version</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/repositories.html">CodeIgniter Repositories</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Build Your First Application</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">Static Pages</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">News Section</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">Create News Items</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">Conclusion</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../concepts/index.html">CodeIgniter4 Overview</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../concepts/structure.html">Application Structure</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/mvc.html">Models, Views, and Controllers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/autoloader.html">Autoloading Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/services.html">Services</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/factories.html">Factories</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/http.html">Working With HTTP Requests</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/security.html">Security Guidelines</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../general/configuration.html">Configuration</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/urls.html">CodeIgniter URLs</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">Helper Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">Global Functions and Constants</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/logging.html">Logging Information</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/errors.html">Error Handling</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/caching.html">Web Page Caching</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/ajax.html">AJAX Requests</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/modules.html">Code Modules</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/managing_apps.html">Managing your Applications</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/environments.html">Handling Multiple Environments</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../incoming/index.html">Controllers and Routing</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../incoming/routing.html">URI Routing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/controllers.html">Controllers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/filters.html">Controller Filters</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/message.html">HTTP Messages</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/request.html">Request Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/incomingrequest.html">IncomingRequest Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/content_negotiation.html">Content Negotiation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/methodspoofing.html">HTTP Method Spoofing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/restful.html">RESTful Resource Handling</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../outgoing/index.html">Building Responses</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/views.html">Views</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_cells.html">View Cells</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_renderer.html">View Renderer</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_layouts.html">View Layouts</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_parser.html">View Parser</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_decorators.html">View Decorators</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/table.html">HTML Table Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/response.html">HTTP Responses</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/api_responses.html">API Response Trait</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/localization.html">Localization</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/alternative_php.html">Alternate PHP Syntax for View Files</a></li>
</ul>
</li>
</ul>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Working With Databases</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="examples.html">Quick Start: Usage Examples</a></li>
<li class="toctree-l2"><a class="reference internal" href="configuration.html">Database Configuration</a></li>
<li class="toctree-l2"><a class="reference internal" href="connecting.html">Connecting to a Database</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Running Queries</a></li>
<li class="toctree-l2"><a class="reference internal" href="results.html">Generating Query Results</a></li>
<li class="toctree-l2"><a class="reference internal" href="helpers.html">Query Helper Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="query_builder.html">Query Builder Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="transactions.html">Transactions</a></li>
<li class="toctree-l2"><a class="reference internal" href="metadata.html">Getting MetaData</a></li>
<li class="toctree-l2"><a class="reference internal" href="call_function.html">Custom Function Calls</a></li>
<li class="toctree-l2"><a class="reference internal" href="events.html">Database Events</a></li>
<li class="toctree-l2"><a class="reference internal" href="utilities.html">Database Utilities</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../models/index.html">Modeling Data</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../models/model.html">Using CodeIgniter's Model</a></li>
<li class="toctree-l2"><a class="reference internal" href="../models/entities.html">Using Entity Classes</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../dbmgmt/index.html">Managing Databases</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/forge.html">Database Manipulation with Database Forge</a></li>
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/migration.html">Database Migrations</a></li>
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/seeds.html">Database Seeding</a></li>
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/db_commands.html">Database Commands</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Library Reference</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../libraries/caching.html">Caching Driver</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/cookies.html">Cookies</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/curlrequest.html">CURLRequest Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/email.html">Email Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/encryption.html">Encryption Service</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/files.html">Working with Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/files.html#file-collections">File Collections</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/honeypot.html">Honeypot Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/images.html">Image Manipulation Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/pagination.html">Pagination</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/publisher.html">Publisher</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/security.html">Security</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/sessions.html">Session Library</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/throttler.html">Throttler</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/time.html">Times and Dates</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/typography.html">Typography</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/uploaded_files.html">Working with Uploaded Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/uri.html">Working with URIs</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/user_agent.html">User Agent Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../libraries/validation.html">Validation</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">Array Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">Date Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/filesystem_helper.html">Filesystem Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">Form Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">Number Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">Security Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/test_helper.html">Test Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">Text Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML Helper</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../testing/index.html">Testing</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../testing/overview.html">Getting Started</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/database.html">Database</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/fabricator.html">Generating Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/controllers.html">Controller Testing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/feature.html">HTTP Testing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/response.html">Testing Responses</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/benchmark.html">Benchmarking</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/debugging.html">Debugging Your Application</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/mocking.html">Mocking</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../cli/index.html">Command Line Usage</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_overview.html">CLI Overview</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_controllers.html">Running Controllers via CLI</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/spark_commands.html">Spark Commands</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_commands.html">Creating Spark Commands</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_generators.html">CLI Generators</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_library.html">CLI Library</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_request.html">CLIRequest Class</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../extending/index.html">Extending CodeIgniter</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../extending/core_classes.html">Creating Core System Classes</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/common.html">Replacing Common Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/events.html">Events</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/basecontroller.html">Extending the Controller</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/authentication.html">Authentication</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/contributing.html">Contributing to CodeIgniter</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../libraries/official_packages.html">Official Packages</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" style="background: #DD4814" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">CodeIgniter</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<a class="btn btn-neutral float-right" href="https://github.com/codeigniter4/CodeIgniter4/edit/develop/user_guide_src/source/database/queries.rst">Edit this page</a>
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
<li><a href="index.html">Working With Databases</a> &raquo;</li>
<li>Queries</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<section id="queries">
<h1>Queries<a class="headerlink" href="#queries" title="Permalink to this headline"></a></h1>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><p><a class="reference internal" href="#query-basics" id="id2">Query Basics</a></p>
<ul>
<li><p><a class="reference internal" href="#regular-queries" id="id3">Regular Queries</a></p></li>
<li><p><a class="reference internal" href="#simplified-queries" id="id4">Simplified Queries</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#working-with-database-prefixes-manually" id="id5">Working with Database Prefixes Manually</a></p></li>
<li><p><a class="reference internal" href="#protecting-identifiers" id="id6">Protecting Identifiers</a></p></li>
<li><p><a class="reference internal" href="#escaping-values" id="id7">Escaping Values</a></p>
<ul>
<li><p><a class="reference internal" href="#db-escape" id="id8">1. $db-&gt;escape()</a></p></li>
<li><p><a class="reference internal" href="#db-escapestring" id="id9">2. $db-&gt;escapeString()</a></p></li>
<li><p><a class="reference internal" href="#db-escapelikestring" id="id10">3. $db-&gt;escapeLikeString()</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#query-bindings" id="id11">Query Bindings</a></p>
<ul>
<li><p><a class="reference internal" href="#named-bindings" id="id12">Named Bindings</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#handling-errors" id="id13">Handling Errors</a></p>
<ul>
<li><p><a class="reference internal" href="#db-error" id="id14">$db-&gt;error()</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#prepared-queries" id="id15">Prepared Queries</a></p>
<ul>
<li><p><a class="reference internal" href="#preparing-the-query" id="id16">Preparing the Query</a></p></li>
<li><p><a class="reference internal" href="#executing-the-query" id="id17">Executing the Query</a></p></li>
<li><p><a class="reference internal" href="#other-methods" id="id18">Other Methods</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#working-with-query-objects" id="id19">Working with Query Objects</a></p>
<ul>
<li><p><a class="reference internal" href="#getlastquery" id="id20">getLastQuery()</a></p></li>
<li><p><a class="reference internal" href="#the-query-class" id="id21">The Query Class</a></p></li>
</ul>
</li>
</ul>
</div>
<section id="query-basics">
<h2><a class="toc-backref" href="#id2">Query Basics</a><a class="headerlink" href="#query-basics" title="Permalink to this headline"></a></h2>
<section id="regular-queries">
<h3><a class="toc-backref" href="#id3">Regular Queries</a><a class="headerlink" href="#regular-queries" title="Permalink to this headline"></a></h3>
<p>To submit a query, use the <strong>query</strong> function:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$db</span> <span class="o">=</span> <span class="nx">db_connect</span><span class="p">();</span>
<span class="nv">$db</span><span class="o">-&gt;</span><span class="na">query</span><span class="p">(</span><span class="s1">&#39;YOUR QUERY HERE&#39;</span><span class="p">);</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">query()</span></code> function returns a database result <strong>object</strong> when “read”
type queries are run which you can use to <a class="reference internal" href="results.html"><span class="doc">show your
results</span></a>. When “write” type queries are run it simply
returns true or false depending on success or failure. When retrieving
data you will typically assign the query to your own variable, like
this:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$db</span><span class="o">-&gt;</span><span class="na">query</span><span class="p">(</span><span class="s1">&#39;YOUR QUERY HERE&#39;</span><span class="p">);</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>If you are using OCI8 Driver, SQL statements should not end with a semi-colon (<code class="docutils literal notranslate"><span class="pre">;</span></code>).
PL/SQL statements should end with a semi-colon (<code class="docutils literal notranslate"><span class="pre">;</span></code>).</p>
</div>
</section>
<section id="simplified-queries">
<h3><a class="toc-backref" href="#id4">Simplified Queries</a><a class="headerlink" href="#simplified-queries" title="Permalink to this headline"></a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">simpleQuery()</span></code> method is a simplified version of the
<code class="docutils literal notranslate"><span class="pre">$db-&gt;query()</span></code> method. It DOES
NOT return a database result set, nor does it set the query timer, or
compile bind data, or store your query for debugging. It simply lets you
submit a query. Most users will rarely use this function.</p>
<p>It returns whatever the database drivers “execute” function returns.
That typically is true/false on success or failure for write type queries
such as INSERT, DELETE or UPDATE statements (which is what it really
should be used for) and a resource/object on success for queries with
fetchable results.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">$db</span><span class="o">-&gt;</span><span class="na">simpleQuery</span><span class="p">(</span><span class="s1">&#39;YOUR QUERY&#39;</span><span class="p">))</span> <span class="p">{</span>
<span class="k">echo</span> <span class="s1">&#39;Success!&#39;</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="k">echo</span> <span class="s1">&#39;Query failed!&#39;</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>PostgreSQLs <code class="docutils literal notranslate"><span class="pre">pg_exec()</span></code> function (for example) always
returns a resource on success even for write type queries.
So keep that in mind if youre looking for a boolean value.</p>
</div>
</section>
</section>
<section id="working-with-database-prefixes-manually">
<h2><a class="toc-backref" href="#id5">Working with Database Prefixes Manually</a><a class="headerlink" href="#working-with-database-prefixes-manually" title="Permalink to this headline"></a></h2>
<p>If you have configured a database prefix and would like to prepend it to
a table name for use in a native SQL query for example, then you can use
the following:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$db</span><span class="o">-&gt;</span><span class="na">prefixTable</span><span class="p">(</span><span class="s1">&#39;tablename&#39;</span><span class="p">);</span> <span class="c1">// outputs prefix_tablename</span>
</pre></div>
</div>
<p>If for any reason you would like to change the prefix programmatically
without needing to create a new connection you can use this method:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$db</span><span class="o">-&gt;</span><span class="na">setPrefix</span><span class="p">(</span><span class="s1">&#39;newprefix_&#39;</span><span class="p">);</span>
<span class="nv">$db</span><span class="o">-&gt;</span><span class="na">prefixTable</span><span class="p">(</span><span class="s1">&#39;tablename&#39;</span><span class="p">);</span> <span class="c1">// outputs newprefix_tablename</span>
</pre></div>
</div>
<p>You can get the current prefix any time with this method:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$DBPrefix</span> <span class="o">=</span> <span class="nv">$db</span><span class="o">-&gt;</span><span class="na">getPrefix</span><span class="p">();</span>
</pre></div>
</div>
</section>
<section id="protecting-identifiers">
<h2><a class="toc-backref" href="#id6">Protecting Identifiers</a><a class="headerlink" href="#protecting-identifiers" title="Permalink to this headline"></a></h2>
<p>In many databases, it is advisable to protect table and field names - for
example with backticks in MySQL. <strong>Query Builder queries are
automatically protected</strong>, but if you need to manually protect an
identifier you can use:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$db</span><span class="o">-&gt;</span><span class="na">protectIdentifiers</span><span class="p">(</span><span class="s1">&#39;table_name&#39;</span><span class="p">);</span>
</pre></div>
</div>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>Although the Query Builder will try its best to properly
quote any field and table names that you feed it. Note that it
is NOT designed to work with arbitrary user input. DO NOT feed it
with unsanitized user data.</p>
</div>
<p>This function will also add a <strong>table prefix</strong> to your table, assuming you
have a prefix specified in your database config file. To enable the
prefixing set <code class="docutils literal notranslate"><span class="pre">true</span></code> (boolean) via the second parameter:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$db</span><span class="o">-&gt;</span><span class="na">protectIdentifiers</span><span class="p">(</span><span class="s1">&#39;table_name&#39;</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="escaping-values">
<h2><a class="toc-backref" href="#id7">Escaping Values</a><a class="headerlink" href="#escaping-values" title="Permalink to this headline"></a></h2>
<p>Its a very good security practice to escape your data before submitting
it into your database. CodeIgniter has three methods that help you do
this:</p>
<section id="db-escape">
<h3><a class="toc-backref" href="#id8">1. $db-&gt;escape()</a><a class="headerlink" href="#db-escape" title="Permalink to this headline"></a></h3>
<p>This function determines the data type so
that it can escape only string data. It also automatically adds
single quotes around the data so you dont have to:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$sql</span> <span class="o">=</span> <span class="s1">&#39;INSERT INTO table (title) VALUES(&#39;</span> <span class="o">.</span> <span class="nv">$db</span><span class="o">-&gt;</span><span class="na">escape</span><span class="p">(</span><span class="nv">$title</span><span class="p">)</span> <span class="o">.</span> <span class="s1">&#39;)&#39;</span><span class="p">;</span>
</pre></div>
</div>
</section>
<section id="db-escapestring">
<h3><a class="toc-backref" href="#id9">2. $db-&gt;escapeString()</a><a class="headerlink" href="#db-escapestring" title="Permalink to this headline"></a></h3>
<p>This function escapes the data passed to
it, regardless of type. Most of the time youll use the above
function rather than this one. Use the function like this:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$sql</span> <span class="o">=</span> <span class="s2">&quot;INSERT INTO table (title) VALUES(&#39;&quot;</span> <span class="o">.</span> <span class="nv">$db</span><span class="o">-&gt;</span><span class="na">escapeString</span><span class="p">(</span><span class="nv">$title</span><span class="p">)</span> <span class="o">.</span> <span class="s2">&quot;&#39;)&quot;</span><span class="p">;</span>
</pre></div>
</div>
</section>
<section id="db-escapelikestring">
<h3><a class="toc-backref" href="#id10">3. $db-&gt;escapeLikeString()</a><a class="headerlink" href="#db-escapelikestring" title="Permalink to this headline"></a></h3>
<p>This method should be used when
strings are to be used in LIKE conditions so that LIKE wildcards
(<code class="docutils literal notranslate"><span class="pre">%</span></code>, <code class="docutils literal notranslate"><span class="pre">_</span></code>) in the string are also properly escaped.</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$search</span> <span class="o">=</span> <span class="s1">&#39;20% raise&#39;</span><span class="p">;</span>
<span class="nv">$sql</span> <span class="o">=</span> <span class="s2">&quot;SELECT id FROM table WHERE column LIKE &#39;%&quot;</span> <span class="o">.</span>
<span class="nv">$db</span><span class="o">-&gt;</span><span class="na">escapeLikeString</span><span class="p">(</span><span class="nv">$search</span><span class="p">)</span> <span class="o">.</span> <span class="s2">&quot;%&#39; ESCAPE &#39;!&#39;&quot;</span><span class="p">;</span>
</pre></div>
</div>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>The <code class="docutils literal notranslate"><span class="pre">escapeLikeString()</span></code> method uses <code class="docutils literal notranslate"><span class="pre">'!'</span></code> (exclamation mark)
to escape special characters for <code class="docutils literal notranslate"><span class="pre">LIKE</span></code> conditions. Because this
method escapes partial strings that you would wrap in quotes
yourself, it cannot automatically add the <code class="docutils literal notranslate"><span class="pre">ESCAPE</span> <span class="pre">'!'</span></code>
condition for you, and so youll have to manually do that.</p>
</div>
</section>
</section>
<section id="query-bindings">
<h2><a class="toc-backref" href="#id11">Query Bindings</a><a class="headerlink" href="#query-bindings" title="Permalink to this headline"></a></h2>
<p>Bindings enable you to simplify your query syntax by letting the system
put the queries together for you. Consider the following example:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$sql</span> <span class="o">=</span> <span class="s1">&#39;SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?&#39;</span><span class="p">;</span>
<span class="nv">$db</span><span class="o">-&gt;</span><span class="na">query</span><span class="p">(</span><span class="nv">$sql</span><span class="p">,</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="s1">&#39;live&#39;</span><span class="p">,</span> <span class="s1">&#39;Rick&#39;</span><span class="p">]);</span>
</pre></div>
</div>
<p>The question marks in the query are automatically replaced with the
values in the array in the second parameter of the query function.</p>
<p>Binding also work with arrays, which will be transformed to IN sets:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$sql</span> <span class="o">=</span> <span class="s1">&#39;SELECT * FROM some_table WHERE id IN ? AND status = ? AND author = ?&#39;</span><span class="p">;</span>
<span class="nv">$db</span><span class="o">-&gt;</span><span class="na">query</span><span class="p">(</span><span class="nv">$sql</span><span class="p">,</span> <span class="p">[[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">6</span><span class="p">],</span> <span class="s1">&#39;live&#39;</span><span class="p">,</span> <span class="s1">&#39;Rick&#39;</span><span class="p">]);</span>
</pre></div>
</div>
<p>The resulting query will be:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nx">SELECT</span> <span class="o">*</span> <span class="nx">FROM</span> <span class="nx">some_table</span> <span class="nx">WHERE</span> <span class="nx">id</span> <span class="nx">IN</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">6</span><span class="p">)</span> <span class="k">AND</span> <span class="nx">status</span> <span class="o">=</span> <span class="s1">&#39;live&#39;</span> <span class="k">AND</span> <span class="nx">author</span> <span class="o">=</span> <span class="s1">&#39;Rick&#39;</span>
</pre></div>
</div>
<p>The secondary benefit of using binds is that the values are
automatically escaped producing safer queries.
You dont have to remember to manually escape data - the engine does it automatically for you.</p>
<section id="named-bindings">
<h3><a class="toc-backref" href="#id12">Named Bindings</a><a class="headerlink" href="#named-bindings" title="Permalink to this headline"></a></h3>
<p>Instead of using the question mark to mark the location of the bound values,
you can name the bindings, allowing the keys of the values passed in to match
placeholders in the query:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$sql</span> <span class="o">=</span> <span class="s1">&#39;SELECT * FROM some_table WHERE id = :id: AND status = :status: AND author = :name:&#39;</span><span class="p">;</span>
<span class="nv">$db</span><span class="o">-&gt;</span><span class="na">query</span><span class="p">(</span><span class="nv">$sql</span><span class="p">,</span> <span class="p">[</span>
<span class="s1">&#39;id&#39;</span> <span class="o">=&gt;</span> <span class="mi">3</span><span class="p">,</span>
<span class="s1">&#39;status&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;live&#39;</span><span class="p">,</span>
<span class="s1">&#39;name&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Rick&#39;</span><span class="p">,</span>
<span class="p">]);</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Each name in the query MUST be surrounded by colons.</p>
</div>
</section>
</section>
<section id="handling-errors">
<h2><a class="toc-backref" href="#id13">Handling Errors</a><a class="headerlink" href="#handling-errors" title="Permalink to this headline"></a></h2>
<section id="db-error">
<h3><a class="toc-backref" href="#id14">$db-&gt;error()</a><a class="headerlink" href="#db-error" title="Permalink to this headline"></a></h3>
<p>If you need to get the last error that has occurred, the <code class="docutils literal notranslate"><span class="pre">error()</span></code> method
will return an array containing its code and message. Heres a quick
example:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span> <span class="nv">$db</span><span class="o">-&gt;</span><span class="na">simpleQuery</span><span class="p">(</span><span class="s1">&#39;SELECT `example_field` FROM `example_table`&#39;</span><span class="p">))</span> <span class="p">{</span>
<span class="nv">$error</span> <span class="o">=</span> <span class="nv">$db</span><span class="o">-&gt;</span><span class="na">error</span><span class="p">();</span> <span class="c1">// Has keys &#39;code&#39; and &#39;message&#39;</span>
<span class="p">}</span>
</pre></div>
</div>
</section>
</section>
<section id="prepared-queries">
<h2><a class="toc-backref" href="#id15">Prepared Queries</a><a class="headerlink" href="#prepared-queries" title="Permalink to this headline"></a></h2>
<p>Most database engines support some form of prepared statements, that allow you to prepare a query once, and then run
that query multiple times with new sets of data. This eliminates the possibility of SQL injection since the data is
passed to the database in a different format than the query itself. When you need to run the same query multiple times
it can be quite a bit faster, too. However, to use it for every query can have major performance hits, since youre calling
out to the database twice as often. Since the Query Builder and Database connections already handle escaping the data
for you, the safety aspect is already taken care of for you. There will be times, though, when you need to ability
to optimize the query by running a prepared statement, or prepared query.</p>
<section id="preparing-the-query">
<h3><a class="toc-backref" href="#id16">Preparing the Query</a><a class="headerlink" href="#preparing-the-query" title="Permalink to this headline"></a></h3>
<p>This can be easily done with the <code class="docutils literal notranslate"><span class="pre">prepare()</span></code> method. This takes a single parameter, which is a Closure that returns
a query object. Query objects are automatically generated by any of the “final” type queries, including <strong>insert</strong>,
<strong>update</strong>, <strong>delete</strong>, <strong>replace</strong>, and <strong>get</strong>. This is handled the easiest by using the Query Builder to
run a query. The query is not actually run, and the values dont matter since theyre never applied, acting instead
as placeholders. This returns a PreparedQuery object:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$pQuery</span> <span class="o">=</span> <span class="nv">$db</span><span class="o">-&gt;</span><span class="na">prepare</span><span class="p">(</span><span class="k">static</span> <span class="k">function</span> <span class="p">(</span><span class="nv">$db</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nv">$db</span><span class="o">-&gt;</span><span class="na">table</span><span class="p">(</span><span class="s1">&#39;user&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">insert</span><span class="p">([</span>
<span class="s1">&#39;name&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;x&#39;</span><span class="p">,</span>
<span class="s1">&#39;email&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;y&#39;</span><span class="p">,</span>
<span class="s1">&#39;country&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;US&#39;</span><span class="p">,</span>
<span class="p">]);</span>
<span class="p">});</span>
</pre></div>
</div>
<p>If you dont want to use the Query Builder you can create the Query object manually using question marks for
value placeholders:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">use</span> <span class="nx">CodeIgniter\Database\Query</span><span class="p">;</span>
<span class="nv">$pQuery</span> <span class="o">=</span> <span class="nv">$db</span><span class="o">-&gt;</span><span class="na">prepare</span><span class="p">(</span><span class="k">static</span> <span class="k">function</span> <span class="p">(</span><span class="nv">$db</span><span class="p">)</span> <span class="p">{</span>
<span class="nv">$sql</span> <span class="o">=</span> <span class="s1">&#39;INSERT INTO user (name, email, country) VALUES (?, ?, ?)&#39;</span><span class="p">;</span>
<span class="k">return</span> <span class="p">(</span><span class="k">new</span> <span class="nx">Query</span><span class="p">(</span><span class="nv">$db</span><span class="p">))</span><span class="o">-&gt;</span><span class="na">setQuery</span><span class="p">(</span><span class="nv">$sql</span><span class="p">);</span>
<span class="p">});</span>
</pre></div>
</div>
<p>If the database requires an array of options passed to it during the prepare statement phase you can pass that
array through in the second parameter:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">use</span> <span class="nx">CodeIgniter\Database\Query</span><span class="p">;</span>
<span class="nv">$pQuery</span> <span class="o">=</span> <span class="nv">$db</span><span class="o">-&gt;</span><span class="na">prepare</span><span class="p">(</span><span class="k">static</span> <span class="k">function</span> <span class="p">(</span><span class="nv">$db</span><span class="p">)</span> <span class="p">{</span>
<span class="nv">$sql</span> <span class="o">=</span> <span class="s1">&#39;INSERT INTO user (name, email, country) VALUES (?, ?, ?)&#39;</span><span class="p">;</span>
<span class="k">return</span> <span class="p">(</span><span class="k">new</span> <span class="nx">Query</span><span class="p">(</span><span class="nv">$db</span><span class="p">))</span><span class="o">-&gt;</span><span class="na">setQuery</span><span class="p">(</span><span class="nv">$sql</span><span class="p">);</span>
<span class="p">},</span> <span class="nv">$options</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="executing-the-query">
<h3><a class="toc-backref" href="#id17">Executing the Query</a><a class="headerlink" href="#executing-the-query" title="Permalink to this headline"></a></h3>
<p>Once you have a prepared query you can use the <code class="docutils literal notranslate"><span class="pre">execute()</span></code> method to actually run the query. You can pass in as
many variables as you need in the query parameters. The number of parameters you pass must match the number of
placeholders in the query. They must also be passed in the same order as the placeholders appear in the original
query:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="c1">// Prepare the Query</span>
<span class="nv">$pQuery</span> <span class="o">=</span> <span class="nv">$db</span><span class="o">-&gt;</span><span class="na">prepare</span><span class="p">(</span><span class="k">static</span> <span class="k">function</span> <span class="p">(</span><span class="nv">$db</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nv">$db</span><span class="o">-&gt;</span><span class="na">table</span><span class="p">(</span><span class="s1">&#39;user&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">insert</span><span class="p">([</span>
<span class="s1">&#39;name&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;x&#39;</span><span class="p">,</span>
<span class="s1">&#39;email&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;y&#39;</span><span class="p">,</span>
<span class="s1">&#39;country&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;US&#39;</span><span class="p">,</span>
<span class="p">]);</span>
<span class="p">});</span>
<span class="c1">// Collect the Data</span>
<span class="nv">$name</span> <span class="o">=</span> <span class="s1">&#39;John Doe&#39;</span><span class="p">;</span>
<span class="nv">$email</span> <span class="o">=</span> <span class="s1">&#39;j.doe@example.com&#39;</span><span class="p">;</span>
<span class="nv">$country</span> <span class="o">=</span> <span class="s1">&#39;US&#39;</span><span class="p">;</span>
<span class="c1">// Run the Query</span>
<span class="nv">$results</span> <span class="o">=</span> <span class="nv">$pQuery</span><span class="o">-&gt;</span><span class="na">execute</span><span class="p">(</span><span class="nv">$name</span><span class="p">,</span> <span class="nv">$email</span><span class="p">,</span> <span class="nv">$country</span><span class="p">);</span>
</pre></div>
</div>
<p>This returns a standard <a class="reference internal" href="results.html"><span class="doc">result set</span></a>.</p>
</section>
<section id="other-methods">
<h3><a class="toc-backref" href="#id18">Other Methods</a><a class="headerlink" href="#other-methods" title="Permalink to this headline"></a></h3>
<p>In addition to these two primary methods, the prepared query object also has the following methods:</p>
<section id="close">
<h4>close()<a class="headerlink" href="#close" title="Permalink to this headline"></a></h4>
<p>While PHP does a pretty good job of closing all open statements with the database its always a good idea to
close out the prepared statement when youre done with it:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$pQuery</span><span class="o">-&gt;</span><span class="na">close</span><span class="p">();</span>
</pre></div>
</div>
</section>
<section id="getquerystring">
<h4>getQueryString()<a class="headerlink" href="#getquerystring" title="Permalink to this headline"></a></h4>
<p>This returns the prepared query as a string.</p>
</section>
<section id="haserror">
<h4>hasError()<a class="headerlink" href="#haserror" title="Permalink to this headline"></a></h4>
<p>Returns boolean true/false if the last <code class="docutils literal notranslate"><span class="pre">execute()</span></code> call created any errors.</p>
</section>
<section id="geterrorcode-geterrormessage">
<h4>getErrorCode() getErrorMessage()<a class="headerlink" href="#geterrorcode-geterrormessage" title="Permalink to this headline"></a></h4>
<p>If any errors were encountered these methods can be used to retrieve the error code and string.</p>
</section>
</section>
</section>
<section id="working-with-query-objects">
<h2><a class="toc-backref" href="#id19">Working with Query Objects</a><a class="headerlink" href="#working-with-query-objects" title="Permalink to this headline"></a></h2>
<p>Internally, all queries are processed and stored as instances of
<code class="docutils literal notranslate"><span class="pre">CodeIgniter\Database\Query</span></code>. This class is responsible for binding
the parameters, otherwise preparing the query, and storing performance
data about its query.</p>
<section id="getlastquery">
<h3><a class="toc-backref" href="#id20">getLastQuery()</a><a class="headerlink" href="#getlastquery" title="Permalink to this headline"></a></h3>
<p>When you just need to retrieve the last Query object, use the
<code class="docutils literal notranslate"><span class="pre">getLastQuery()</span></code> method:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$query</span> <span class="o">=</span> <span class="nv">$db</span><span class="o">-&gt;</span><span class="na">getLastQuery</span><span class="p">();</span>
<span class="k">echo</span> <span class="p">(</span><span class="nx">string</span><span class="p">)</span> <span class="nv">$query</span><span class="p">;</span>
</pre></div>
</div>
</section>
<section id="the-query-class">
<h3><a class="toc-backref" href="#id21">The Query Class</a><a class="headerlink" href="#the-query-class" title="Permalink to this headline"></a></h3>
<p>Each query object stores several pieces of information about the query itself.
This is used, in part, by the Timeline feature, but is available for your use
as well.</p>
<section id="getquery">
<h4>getQuery()<a class="headerlink" href="#getquery" title="Permalink to this headline"></a></h4>
<p>Returns the final query after all processing has happened. This is the exact
query that was sent to the database:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$sql</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-&gt;</span><span class="na">getQuery</span><span class="p">();</span>
</pre></div>
</div>
<p>This same value can be retrieved by casting the Query object to a string:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$sql</span> <span class="o">=</span> <span class="p">(</span><span class="nx">string</span><span class="p">)</span> <span class="nv">$query</span><span class="p">;</span>
</pre></div>
</div>
</section>
<section id="getoriginalquery">
<h4>getOriginalQuery()<a class="headerlink" href="#getoriginalquery" title="Permalink to this headline"></a></h4>
<p>Returns the raw SQL that was passed into the object. This will not have any
binds in it, or prefixes swapped out, etc:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$sql</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-&gt;</span><span class="na">getOriginalQuery</span><span class="p">();</span>
</pre></div>
</div>
</section>
<section id="id1">
<h4>hasError()<a class="headerlink" href="#id1" title="Permalink to this headline"></a></h4>
<p>If an error was encountered during the execution of this query this method
will return true:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">$query</span><span class="o">-&gt;</span><span class="na">hasError</span><span class="p">())</span> <span class="p">{</span>
<span class="k">echo</span> <span class="s1">&#39;Code: &#39;</span> <span class="o">.</span> <span class="nv">$query</span><span class="o">-&gt;</span><span class="na">getErrorCode</span><span class="p">();</span>
<span class="k">echo</span> <span class="s1">&#39;Error: &#39;</span> <span class="o">.</span> <span class="nv">$query</span><span class="o">-&gt;</span><span class="na">getErrorMessage</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
</section>
<section id="iswritetype">
<h4>isWriteType()<a class="headerlink" href="#iswritetype" title="Permalink to this headline"></a></h4>
<p>Returns true if the query was determined to be a write-type query (i.e.,
INSERT, UPDATE, DELETE, etc):</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">$query</span><span class="o">-&gt;</span><span class="na">isWriteType</span><span class="p">())</span> <span class="p">{</span>
<span class="c1">// ... do something</span>
<span class="p">}</span>
</pre></div>
</div>
</section>
<section id="swapprefix">
<h4>swapPrefix()<a class="headerlink" href="#swapprefix" title="Permalink to this headline"></a></h4>
<p>Replaces one table prefix with another value in the SQL. The first
parameter is the original prefix that you want replaced, and the second
parameter is the value you want it replaced with:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$sql</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-&gt;</span><span class="na">swapPrefix</span><span class="p">(</span><span class="s1">&#39;ci3_&#39;</span><span class="p">,</span> <span class="s1">&#39;ci4_&#39;</span><span class="p">);</span>
</pre></div>
</div>
</section>
<section id="getstarttime">
<h4>getStartTime()<a class="headerlink" href="#getstarttime" title="Permalink to this headline"></a></h4>
<p>Gets the time the query was executed in seconds with microseconds:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$microtime</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-&gt;</span><span class="na">getStartTime</span><span class="p">();</span>
</pre></div>
</div>
</section>
<section id="getduration">
<h4>getDuration()<a class="headerlink" href="#getduration" title="Permalink to this headline"></a></h4>
<p>Returns a float with the duration of the query in seconds with microseconds:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>
<span class="nv">$microtime</span> <span class="o">=</span> <span class="nv">$query</span><span class="o">-&gt;</span><span class="na">getDuration</span><span class="p">();</span>
</pre></div>
</div>
</section>
</section>
</section>
</section>
</div>
</div>
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
<a href="connecting.html" class="btn btn-neutral float-left" title="Connecting to your Database" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="results.html" class="btn btn-neutral float-right" title="Generating Query Results" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<div role="contentinfo">
<p>&#169; Copyright 2019-2022 CodeIgniter Foundation.
<span class="lastupdated">Last updated on Sep 30, 2022.
</span></p>
</div>
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(false);
});
</script>
</body>
</html>