get_where()

public function get_where(int $id, string $table): object|false

Description

Fetches a single record by its ID from a specified database table. This method validates that the table exists, then executes a parameterized query to retrieve the record. Returns an object representation of the record if found, or false if no record matches the ID.

Parameters

Parameter Type Description Default Required
id int The ID of the record to fetch. - 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 record is found.

Exceptions

  • RuntimeException - If the specified table does not exist in the database.

Example #1

The code sample below demonstrates how to fetch a single user record with ID 15 from the 'users' table.

Example #2

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

Important Notes

  • Both $id and $table parameters are required.
  • The method validates that the table exists before executing the query.
  • Uses prepared statements with parameter binding to prevent SQL injection.
  • Returns false (not null) when no record is found.
  • Returned objects have properties corresponding to database column names.