Codeigniter 4 – Query Builder class with Example

Query Builder Basics

- The Query Builder class provides a simple and intuitive way to build and execute database queries.
- Use the db_connect() function to connect to a database and then use the query builder to execute queries.

Example:

$db = db_connect();
$db->table('my_table')->get();

Selecting Data

- Use the select() method to select specific columns.

Example:

$db->table('my_table')->select('name, email')->get();

Selecting All Data

- Use the get() method to select all columns.

Example:

$db->table('my_table')->get();

Filtering Data

- Use the where() method to filter data based on conditions.

Example:

$db->table('my_table')->where('name', 'John Doe')->get();

Filtering Data with Multiple Conditions

- Use the where() method with multiple conditions.

Example:

$db->table('my_table')->where('name', 'John Doe')->where('email', 'john@example.com')->get();

Joining Tables

- Use the join() method to join multiple tables.

Example:

$db->table('my_table')->join('another_table', '(link unavailable) = (link unavailable)')->get();

Grouping Data

- Use the groupBy() method to group data based on columns.

Example:

$db->table('my_table')->groupBy('name')->get();

Sorting Data

- Use the orderBy() method to sort data based on columns.

Example:

$db->table('my_table')->orderBy('name', 'ASC')->get();

Limiting Data

- Use the limit() method to limit the number of rows returned.

Example:

$db->table('my_table')->limit(10)->get();

Offsetting Data

- Use the offset() method to offset the starting point of the data.

Example:

$db->table('my_table')->limit(10)->offset(5)->get();

I hope this helps! Let me know if you have any further questions or need more information.

Leave a Reply

Shopping cart0
There are no products in the cart!
Continue shopping
0