Trongate Docs
switch to dark modeswitch to dark mode
»
»
The 'Query' Method

The 'Query' Method

The 'Query' method executes custom SQL queries.

WARNING!
The 'Query' method offers no protection whatsoever against MySQL injection attacks!  Use with extreme caution!

The following parameters can be accepted by the 'Query' method:

  • $sql (required) - the SQL query that you would like to execute on your database.
  • $return_type (optional) - the data type for table rows that are returned from the query.  Acceptable values for this are 'object' and 'array'.

Just To Let You Know
If you leave $return_type empty then any table rows that are returned from your query will be key, value pair arrays.

    What Gets Returned?

    WHEN MATCHING RECORDS ARE FOUND

    If matching records are found then a PHP array will be returned where each item on the array represents a row from a database table. Each row, within the array, will either be a PHP object or a PHP array.

    WHEN MATCHING RECORDS ARE NOT FOUND

    If the SQL query produces no results then an empty array will be returned.

    Example

    The code below shows an example of a custom SQL query being executed by use of the 'Query' method.  Here, we're attempting to fetch records with a table join then we are immediately displaying fetched records by using Trongate's built-in json() method.  

    <?php
    class Members extends Trongate {

        function test() {
            $sql = 'SELECT
                        module_market_items.*,
                        publishers.publisher_name,
                        members.username
                    FROM
                        module_market_items
                    INNER JOIN
                        publishers
                    ON
                        module_market_items.publishers_id = publishers.id
                    INNER JOIN
                        members
                    ON
                        publishers.members_id = members.id';
            $rows = $this->model->query($sql, 'object');

            json($rows);
        }

    Top Tip
    Create complex table joins in seconds with the Trongate Graphical Query builder.


    HELP & SUPPORT

    If you have a question or a comment relating to anything you've see here, please goto the Help Bar.

     
    ×