get_one_where()

public function get_one_where(string $column, mixed $value, string $table): object|false

Description

Fetches a single record based on a column value from a specified database table. This method validates the table existence and column name, then executes a parameterized query to retrieve the record where the specified column matches the provided value. Returns an object representation of the record if found, or false if no matching record exists.

Parameters

Parameter Type Description Default Required
column string The name of the column to filter by. Must contain only alphanumeric characters and underscores. - Yes
value mixed The value to match against the specified column. Can be any data type (string, int, float, etc.). - Yes
table string The name of the database table to be queried. - Yes

Return Value

Type Description
object|false An object representing the fetched record (using PDO::FETCH_OBJ), or false if no matching record is found.

Exceptions

  • RuntimeException - If the specified table does not exist in the database.
  • InvalidArgumentException - If the column name contains invalid characters (prevents SQL injection).

Example #1

The code sample below demonstrates how to fetch a user record by email address from the 'users' table.

Example #2

The code sample below demonstrates how to fetch a product by SKU from the 'products' table and check if it exists.

Example #3

The code sample below demonstrates how to fetch a record by numeric ID from a custom table.

Important Notes

  • All three parameters ($column, $value, and $table) are required.
  • Column names are validated using regex to prevent SQL injection attacks.
  • The method validates that the table exists before executing the query.
  • Uses prepared statements with parameter binding for security.
  • Returns only the first matching record if multiple records match the criteria.
  • Returns false (not null) when no matching record is found.