get_where_in()
public function get_where_in(string $table, string $column, array $values, array $options = []): array
Description
Retrieves records from a database table where a column's value matches any value in a specified array. This method is useful for fetching multiple records with specific IDs or values in a single query. It validates table existence, uses parameter binding for security, and supports custom column selection and return types.
Parameters
| Parameter | Type | Description | Default | Required |
|---|---|---|---|---|
| table | string | The name of the database table to query. | - | Yes |
| column | string | The name of the column to filter by. | - | Yes |
| values | array | Array of values to match against the specified column. If empty, returns empty array. | - | Yes |
| options | array | Optional settings array. See Options section below for details. | [] | No |
Options Array
| Key | Type | Description | Default |
|---|---|---|---|
| columns | string|array | Columns to select. Can be string (e.g., 'id, name') or array (e.g., ['id', 'name']). | '*' |
| return_type | string | Result format: 'object' for objects or 'array' for associative arrays. | 'object' |
Return Value
| Type | Description |
|---|---|
| array | Array of objects or associative arrays representing the fetched rows. Returns empty array if $values array is empty. |
Exceptions
- RuntimeException - If the specified table does not exist in the database.
Example #1
The code sample below demonstrates how to fetch multiple users by their IDs from the 'users' table.
Example #2
The code sample below demonstrates how to fetch products with specific statuses, selecting only certain columns and returning results as associative arrays.
Example #3
The code sample below demonstrates how to fetch orders with specific order numbers, selecting all columns (default) and returning objects (default).
Important Notes
- The first three parameters (
$table,$column, and$values) are required. - Returns an empty array immediately if the
$valuesarray is empty. - The method validates that the table exists before executing the query.
- Uses parameter binding with unique parameter names for each value to prevent SQL injection.
- When
$options['columns']is an array, it's converted to a comma-separated string. - Default behavior selects all columns (*) and returns objects (PDO::FETCH_OBJ).