View file File name : queries.html Content : <!DOCTYPE html> <!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]--> <!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]--> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Queries — CodeIgniter 3.1.11 documentation</title> <link rel="shortcut icon" href="../_static/ci-icon.ico"/> <link href='https://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic|Roboto+Slab:400,700|Inconsolata:400,700&subset=latin,cyrillic' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" /> <link rel="index" title="Index" href="../genindex.html"/> <link rel="search" title="Search" href="../search.html"/> <link rel="top" title="CodeIgniter 3.1.11 documentation" href="../index.html"/> <link rel="up" title="Database Reference" href="index.html"/> <link rel="next" title="Generating Query Results" href="results.html"/> <link rel="prev" title="Connecting to your Database" href="connecting.html"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script> </head> <body class="wy-body-for-nav" role="document"> <div id="nav"> <div id="nav_inner"> <div id="pulldown-menu" class="ciNav"> <ul> <li class="toctree-l1"><a class="reference internal" href="../general/welcome.html">Welcome to CodeIgniter</a></li> </ul> <ul> <li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation Instructions</a><ul> <li class="toctree-l2"><a class="reference internal" href="../installation/downloads.html">Downloading CodeIgniter</a></li> <li class="toctree-l2"><a class="reference internal" href="../installation/index.html">Installation Instructions</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/troubleshooting.html">Troubleshooting</a></li> </ul> </li> </ul> <ul> <li class="toctree-l1"><a class="reference internal" href="../overview/index.html">CodeIgniter Overview</a><ul> <li class="toctree-l2"><a class="reference internal" href="../overview/getting_started.html">Getting Started</a></li> <li class="toctree-l2"><a class="reference internal" href="../overview/at_a_glance.html">CodeIgniter at a Glance</a></li> <li class="toctree-l2"><a class="reference internal" href="../overview/features.html">Supported Features</a></li> <li class="toctree-l2"><a class="reference internal" href="../overview/appflow.html">Application Flow Chart</a></li> <li class="toctree-l2"><a class="reference internal" href="../overview/mvc.html">Model-View-Controller</a></li> <li class="toctree-l2"><a class="reference internal" href="../overview/goals.html">Architectural Goals</a></li> </ul> </li> </ul> <ul> <li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</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="../contributing/index.html">Contributing to CodeIgniter</a><ul> <li class="toctree-l2"><a class="reference internal" href="../documentation/index.html">Writing CodeIgniter Documentation</a></li> <li class="toctree-l2"><a class="reference internal" href="../DCO.html">Developer’s Certificate of Origin 1.1</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/urls.html">CodeIgniter URLs</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/controllers.html">Controllers</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/reserved_names.html">Reserved Names</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/views.html">Views</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/models.html">Models</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">Helpers</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/libraries.html">Using CodeIgniter Libraries</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/creating_libraries.html">Creating Libraries</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/drivers.html">Using CodeIgniter Drivers</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/creating_drivers.html">Creating Drivers</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/core_classes.html">Creating Core System Classes</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/ancillary_classes.html">Creating Ancillary Classes</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/hooks.html">Hooks - Extending the Framework Core</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/autoloader.html">Auto-loading Resources</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">Common Functions</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/compatibility_functions.html">Compatibility Functions</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/routing.html">URI Routing</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">Caching</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/profiling.html">Profiling Your Application</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/cli.html">Running via the CLI</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> <li class="toctree-l2"><a class="reference internal" href="../general/alternative_php.html">Alternate PHP Syntax for View Files</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/security.html">Security</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/styleguide.html">PHP Style Guide</a></li> </ul> </li> </ul> <ul> <li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Libraries</a><ul> <li class="toctree-l2"><a class="reference internal" href="../libraries/benchmark.html">Benchmarking Class</a></li> <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/calendar.html">Calendaring Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/cart.html">Shopping Cart Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/config.html">Config 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/encrypt.html">Encrypt Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/encryption.html">Encryption Library</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/file_uploading.html">File Uploading Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/form_validation.html">Form Validation</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/ftp.html">FTP Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/image_lib.html">Image Manipulation Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/input.html">Input Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/javascript.html">Javascript Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/language.html">Language Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/loader.html">Loader Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/migration.html">Migrations Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/output.html">Output Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/pagination.html">Pagination Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/parser.html">Template Parser Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/security.html">Security Class</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/table.html">HTML Table Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/trackback.html">Trackback Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/typography.html">Typography Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/unit_testing.html">Unit Testing Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/uri.html">URI Class</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/xmlrpc.html">XML-RPC and XML-RPC Server Classes</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/zip.html">Zip Encoding Class</a></li> </ul> </li> </ul> <ul class="current"> <li class="toctree-l1 current"><a class="reference internal" href="index.html">Database Reference</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="caching.html">Query Caching</a></li> <li class="toctree-l2"><a class="reference internal" href="forge.html">Database Manipulation with Database Forge</a></li> <li class="toctree-l2"><a class="reference internal" href="utilities.html">Database Utilities Class</a></li> <li class="toctree-l2"><a class="reference internal" href="db_driver_reference.html">Database Driver Reference</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/captcha_helper.html">CAPTCHA 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/directory_helper.html">Directory Helper</a></li> <li class="toctree-l2"><a class="reference internal" href="../helpers/download_helper.html">Download Helper</a></li> <li class="toctree-l2"><a class="reference internal" href="../helpers/email_helper.html">Email Helper</a></li> <li class="toctree-l2"><a class="reference internal" href="../helpers/file_helper.html">File 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/language_helper.html">Language 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/path_helper.html">Path 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/smiley_helper.html">Smiley Helper</a></li> <li class="toctree-l2"><a class="reference internal" href="../helpers/string_helper.html">String 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/typography_helper.html">Typography 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> </div> </div> </div> <div id="nav2"> <a href="#" id="openToc"> <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAARgAA/+4ADkFkb2JlAGTAAAAAAf/bAIQABAMDAwMDBAMDBAYEAwQGBwUEBAUHCAYGBwYGCAoICQkJCQgKCgwMDAwMCgwMDQ0MDBERERERFBQUFBQUFBQUFAEEBQUIBwgPCgoPFA4ODhQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQU/8AAEQgAKwCaAwERAAIRAQMRAf/EAHsAAQ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8lA8utJ/koJ7WCbBU/LQXOPAFq1koK8B0pag90CggtBBf6qB0UDooHRQOigdFA6KB0UDooHRQOigdFA6KB0UDooI0EaBQf//Z" title="Toggle Table of Contents" alt="Toggle Table of Contents" /> </a> </div> <div class="wy-grid-for-nav"> <nav data-toggle="wy-nav-shift" class="wy-nav-side"> <div class="wy-side-nav-search"> <a href="../index.html" class="fa fa-home"> CodeIgniter</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="main navigation"> <ul> <li class="toctree-l1"><a class="reference internal" href="../general/welcome.html">Welcome to CodeIgniter</a></li> </ul> <ul> <li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation Instructions</a><ul> <li class="toctree-l2"><a class="reference internal" href="../installation/downloads.html">Downloading CodeIgniter</a></li> <li class="toctree-l2"><a class="reference internal" href="../installation/index.html">Installation Instructions</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/troubleshooting.html">Troubleshooting</a></li> </ul> </li> </ul> <ul> <li class="toctree-l1"><a class="reference internal" href="../overview/index.html">CodeIgniter Overview</a><ul> <li class="toctree-l2"><a class="reference internal" href="../overview/getting_started.html">Getting Started</a></li> <li class="toctree-l2"><a class="reference internal" href="../overview/at_a_glance.html">CodeIgniter at a Glance</a></li> <li class="toctree-l2"><a class="reference internal" href="../overview/features.html">Supported Features</a></li> <li class="toctree-l2"><a class="reference internal" href="../overview/appflow.html">Application Flow Chart</a></li> <li class="toctree-l2"><a class="reference internal" href="../overview/mvc.html">Model-View-Controller</a></li> <li class="toctree-l2"><a class="reference internal" href="../overview/goals.html">Architectural Goals</a></li> </ul> </li> </ul> <ul> <li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</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="../contributing/index.html">Contributing to CodeIgniter</a><ul> <li class="toctree-l2"><a class="reference internal" href="../documentation/index.html">Writing CodeIgniter Documentation</a></li> <li class="toctree-l2"><a class="reference internal" href="../DCO.html">Developer’s Certificate of Origin 1.1</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/urls.html">CodeIgniter URLs</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/controllers.html">Controllers</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/reserved_names.html">Reserved Names</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/views.html">Views</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/models.html">Models</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">Helpers</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/libraries.html">Using CodeIgniter Libraries</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/creating_libraries.html">Creating Libraries</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/drivers.html">Using CodeIgniter Drivers</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/creating_drivers.html">Creating Drivers</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/core_classes.html">Creating Core System Classes</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/ancillary_classes.html">Creating Ancillary Classes</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/hooks.html">Hooks - Extending the Framework Core</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/autoloader.html">Auto-loading Resources</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">Common Functions</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/compatibility_functions.html">Compatibility Functions</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/routing.html">URI Routing</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">Caching</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/profiling.html">Profiling Your Application</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/cli.html">Running via the CLI</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> <li class="toctree-l2"><a class="reference internal" href="../general/alternative_php.html">Alternate PHP Syntax for View Files</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/security.html">Security</a></li> <li class="toctree-l2"><a class="reference internal" href="../general/styleguide.html">PHP Style Guide</a></li> </ul> </li> </ul> <ul> <li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Libraries</a><ul> <li class="toctree-l2"><a class="reference internal" href="../libraries/benchmark.html">Benchmarking Class</a></li> <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/calendar.html">Calendaring Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/cart.html">Shopping Cart Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/config.html">Config 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/encrypt.html">Encrypt Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/encryption.html">Encryption Library</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/file_uploading.html">File Uploading Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/form_validation.html">Form Validation</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/ftp.html">FTP Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/image_lib.html">Image Manipulation Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/input.html">Input Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/javascript.html">Javascript Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/language.html">Language Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/loader.html">Loader Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/migration.html">Migrations Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/output.html">Output Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/pagination.html">Pagination Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/parser.html">Template Parser Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/security.html">Security Class</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/table.html">HTML Table Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/trackback.html">Trackback Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/typography.html">Typography Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/unit_testing.html">Unit Testing Class</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/uri.html">URI Class</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/xmlrpc.html">XML-RPC and XML-RPC Server Classes</a></li> <li class="toctree-l2"><a class="reference internal" href="../libraries/zip.html">Zip Encoding Class</a></li> </ul> </li> </ul> <ul class="current"> <li class="toctree-l1 current"><a class="reference internal" href="index.html">Database Reference</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="caching.html">Query Caching</a></li> <li class="toctree-l2"><a class="reference internal" href="forge.html">Database Manipulation with Database Forge</a></li> <li class="toctree-l2"><a class="reference internal" href="utilities.html">Database Utilities Class</a></li> <li class="toctree-l2"><a class="reference internal" href="db_driver_reference.html">Database Driver Reference</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/captcha_helper.html">CAPTCHA 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/directory_helper.html">Directory Helper</a></li> <li class="toctree-l2"><a class="reference internal" href="../helpers/download_helper.html">Download Helper</a></li> <li class="toctree-l2"><a class="reference internal" href="../helpers/email_helper.html">Email Helper</a></li> <li class="toctree-l2"><a class="reference internal" href="../helpers/file_helper.html">File 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/language_helper.html">Language 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/path_helper.html">Path 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/smiley_helper.html">Smiley Helper</a></li> <li class="toctree-l2"><a class="reference internal" href="../helpers/string_helper.html">String 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/typography_helper.html">Typography 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> </div> </nav> <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"> <nav class="wy-nav-top" role="navigation" aria-label="top navigation"> <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"> <div role="navigation" aria-label="breadcrumbs navigation"> <ul class="wy-breadcrumbs"> <li><a href="../index.html">Docs</a> »</li> <li><a href="index.html">Database Reference</a> »</li> <li>Queries</li> <li class="wy-breadcrumbs-aside"> </li> <div style="float:right;margin-left:5px;" id="closeMe"> <img title="Classic Layout" alt="classic layout" src="data:image/gif;base64,R0lGODlhFAAUAJEAAAAAADMzM////wAAACH5BAUUAAIALAAAAAAUABQAAAImlI+py+0PU5gRBRDM3DxbWoXis42X13USOLauUIqnlsaH/eY6UwAAOw==" /> </div> </ul> <hr/> </div> <div role="main" class="document"> <div class="section" id="queries"> <h1>Queries<a class="headerlink" href="#queries" title="Permalink to this headline">¶</a></h1> <div class="section" id="query-basics"> <h2>Query Basics<a class="headerlink" href="#query-basics" title="Permalink to this headline">¶</a></h2> <div class="section" id="regular-queries"> <h3>Regular Queries<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-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">db</span><span class="o">-></span><span class="na">query</span><span class="p">(</span><span class="s1">'YOUR QUERY HERE'</span><span class="p">);</span> </pre></div> </div> <p>The query() 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-ci"><div class="highlight"><pre><span></span><span class="nv">$query</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-></span><span class="na">db</span><span class="o">-></span><span class="na">query</span><span class="p">(</span><span class="s1">'YOUR QUERY HERE'</span><span class="p">);</span> </pre></div> </div> </div> <div class="section" id="simplified-queries"> <h3>Simplified Queries<a class="headerlink" href="#simplified-queries" title="Permalink to this headline">¶</a></h3> <p>The <strong>simple_query</strong> method is a simplified version of the $this->db->query() 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-ci"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">db</span><span class="o">-></span><span class="na">simple_query</span><span class="p">(</span><span class="s1">'YOUR QUERY'</span><span class="p">))</span> <span class="p">{</span> <span class="k">echo</span> <span class="s2">"Success!"</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="s2">"Query failed!"</span><span class="p">;</span> <span class="p">}</span> </pre></div> </div> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">PostgreSQL’s <code class="docutils literal"><span class="pre">pg_exec()</span></code> function (for example) always returns a resource on success, even for write type queries. So take that in mind if you’re looking for a boolean value.</p> </div> </div> </div> <div class="section" id="working-with-database-prefixes-manually"> <h2>Working with Database prefixes manually<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-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">db</span><span class="o">-></span><span class="na">dbprefix</span><span class="p">(</span><span class="s1">'tablename'</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 programatically without needing to create a new connection, you can use this method:</p> <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">db</span><span class="o">-></span><span class="na">set_dbprefix</span><span class="p">(</span><span class="s1">'newprefix_'</span><span class="p">);</span> <span class="nv">$this</span><span class="o">-></span><span class="na">db</span><span class="o">-></span><span class="na">dbprefix</span><span class="p">(</span><span class="s1">'tablename'</span><span class="p">);</span> <span class="c1">// outputs newprefix_tablename</span> </pre></div> </div> </div> <div class="section" id="protecting-identifiers"> <h2>Protecting identifiers<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>, however if you need to manually protect an identifier you can use:</p> <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">db</span><span class="o">-></span><span class="na">protect_identifiers</span><span class="p">(</span><span class="s1">'table_name'</span><span class="p">);</span> </pre></div> </div> <div class="admonition important"> <p class="first admonition-title">Important</p> <p class="last">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 table prefix to your table, assuming you have a prefix specified in your database config file. To enable the prefixing set TRUE (boolean) via the second parameter:</p> <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">db</span><span class="o">-></span><span class="na">protect_identifiers</span><span class="p">(</span><span class="s1">'table_name'</span><span class="p">,</span> <span class="k">TRUE</span><span class="p">);</span> </pre></div> </div> </div> <div class="section" id="escaping-queries"> <h2>Escaping Queries<a class="headerlink" href="#escaping-queries" title="Permalink to this headline">¶</a></h2> <p>It’s 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> <ol class="arabic"> <li><p class="first"><strong>$this->db->escape()</strong> 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 don’t have to:</p> <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$sql</span> <span class="o">=</span> <span class="s2">"INSERT INTO table (title) VALUES("</span><span class="o">.</span><span class="nv">$this</span><span class="o">-></span><span class="na">db</span><span class="o">-></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="s2">")"</span><span class="p">;</span> </pre></div> </div> </li> <li><p class="first"><strong>$this->db->escape_str()</strong> This function escapes the data passed to it, regardless of type. Most of the time you’ll use the above function rather than this one. Use the function like this:</p> <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$sql</span> <span class="o">=</span> <span class="s2">"INSERT INTO table (title) VALUES('"</span><span class="o">.</span><span class="nv">$this</span><span class="o">-></span><span class="na">db</span><span class="o">-></span><span class="na">escape_str</span><span class="p">(</span><span class="nv">$title</span><span class="p">)</span><span class="o">.</span><span class="s2">"')"</span><span class="p">;</span> </pre></div> </div> </li> <li><p class="first"><strong>$this->db->escape_like_str()</strong> This method should be used when strings are to be used in LIKE conditions so that LIKE wildcards (‘%’, ‘_’) in the string are also properly escaped.</p> </li> </ol> <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$search</span> <span class="o">=</span> <span class="s1">'20% raise'</span><span class="p">;</span> <span class="nv">$sql</span> <span class="o">=</span> <span class="s2">"SELECT id FROM table WHERE column LIKE '%"</span> <span class="o">.</span> <span class="nv">$this</span><span class="o">-></span><span class="na">db</span><span class="o">-></span><span class="na">escape_like_str</span><span class="p">(</span><span class="nv">$search</span><span class="p">)</span><span class="o">.</span><span class="s2">"%' ESCAPE '!'"</span><span class="p">;</span> </pre></div> </div> <div class="admonition important"> <p class="first admonition-title">Important</p> <p class="last">The <code class="docutils literal"><span class="pre">escape_like_str()</span></code> method uses ‘!’ (exclamation mark) to escape special characters for <em>LIKE</em> conditions. Because this method escapes partial strings that you would wrap in quotes yourself, it cannot automatically add the <code class="docutils literal"><span class="pre">ESCAPE</span> <span class="pre">'!'</span></code> condition for you, and so you’ll have to manually do that.</p> </div> </div> <div class="section" id="query-bindings"> <h2>Query Bindings<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-ci"><div class="highlight"><pre><span></span><span class="nv">$sql</span> <span class="o">=</span> <span class="s2">"SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?"</span><span class="p">;</span> <span class="nv">$this</span><span class="o">-></span><span class="na">db</span><span class="o">-></span><span class="na">query</span><span class="p">(</span><span class="nv">$sql</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="s1">'live'</span><span class="p">,</span> <span class="s1">'Rick'</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-ci"><div class="highlight"><pre><span></span><span class="nv">$sql</span> <span class="o">=</span> <span class="s2">"SELECT * FROM some_table WHERE id IN ? AND status = ? AND author = ?"</span><span class="p">;</span> <span class="nv">$this</span><span class="o">-></span><span class="na">db</span><span class="o">-></span><span class="na">query</span><span class="p">(</span><span class="nv">$sql</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="k">array</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">'live'</span><span class="p">,</span> <span class="s1">'Rick'</span><span class="p">));</span> </pre></div> </div> <p>The resulting query will be:</p> <div class="highlight-ci"><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">'live'</span> <span class="k">AND</span> <span class="nx">author</span> <span class="o">=</span> <span class="s1">'Rick'</span> </pre></div> </div> <p>The secondary benefit of using binds is that the values are automatically escaped, producing safer queries. You don’t have to remember to manually escape data; the engine does it automatically for you.</p> </div> <div class="section" id="handling-errors"> <h2>Handling Errors<a class="headerlink" href="#handling-errors" title="Permalink to this headline">¶</a></h2> <p><strong>$this->db->error();</strong></p> <p>If you need to get the last error that has occurred, the error() method will return an array containing its code and message. Here’s a quick example:</p> <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="p">(</span> <span class="o">!</span> <span class="nv">$this</span><span class="o">-></span><span class="na">db</span><span class="o">-></span><span class="na">simple_query</span><span class="p">(</span><span class="s1">'SELECT `example_field` FROM `example_table`'</span><span class="p">))</span> <span class="p">{</span> <span class="nv">$error</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-></span><span class="na">db</span><span class="o">-></span><span class="na">error</span><span class="p">();</span> <span class="c1">// Has keys 'code' and 'message'</span> <span class="p">}</span> </pre></div> </div> </div> </div> </div> <footer> <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation"> <a href="results.html" class="btn btn-neutral float-right" title="Generating Query Results">Next <span class="fa fa-arrow-circle-right"></span></a> <a href="connecting.html" class="btn btn-neutral" title="Connecting to your Database"><span class="fa fa-arrow-circle-left"></span> Previous</a> </div> <hr/> <div role="contentinfo"> <p> © Copyright 2014 - 2019, British Columbia Institute of Technology. Last updated on Sep 19, 2019. </p> </div> Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. </footer> </div> </div> </section> </div> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT:'../', VERSION:'3.1.11', COLLAPSE_INDEX:false, FILE_SUFFIX:'.html', HAS_SOURCE: false }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/js/theme.js"></script> <script type="text/javascript"> jQuery(function () { SphinxRtdTheme.StickyNav.enable(); }); </script> </body> </html>