Skip to main content

Get Row Function

The get_row function allows you to filter and retrieve specific rows from the database based on given conditions. This is useful when searching for records that match certain criteria.


How to Use the get_row Function

Basic Usage

Retrieve rows from the database that match specific conditions.

$searchConditions = ["username" => "alice"];
$result = $sdb->get_row("users", $searchConditions);

This will return all rows where the username is "alice".


Advanced Usage with Operators

You can specify conditions using comparison operators:

$searchConditions = [
"age" => [">", 25],
"score" => [">=", 80]
];

$result = $sdb->get_row("users", $searchConditions);

This fetches all users older than 25 and with a score of at least 80.


Combining Multiple Conditions

The function supports AND and OR conditions:

Using AND (All conditions must be met)

$searchConditions = [
"AND" => [
["age" => [">", 18]],
["role" => "admin"]
]
];

$result = $sdb->get_row("users", $searchConditions);

Returns all users above 18 AND with the role "admin".


Using OR (At least one condition must be met)

$searchConditions = [
"OR" => [
["country" => "India"],
["country" => "USA"]
]
];

$result = $sdb->get_row("users", $searchConditions);

Returns all users from India OR USA.


Reversing the Order of Results

Use "reverse" to get results in reverse order:

$result = $sdb->get_row("users", ["age" => [">", 18]], "reverse");

This retrieves all users older than 18 but in reverse order.


Handling Errors

  • If no matching rows are found, get_row returns an empty array.
  • If error_report is enabled, a warning is reported when no data is found.
$result = $sdb->get_row("users", ["username" => "unknown"], "", true);

This will trigger a warning because no user named "unknown" exists.


When to Use get_row

Use get_row when:

  • Searching for specific records in the database.
  • Filtering data based on multiple conditions.
  • Performing complex searches using AND/OR operators.

Do NOT use get_row when:

  • You need to fetch all records (get() is better for that).
  • You want to modify existing records (use an update function instead).

Final Thoughts

The get_row function is a powerful tool for searching data efficiently in SwartzDB. It supports multiple conditions, operators, and logical groupings to help you retrieve exactly what you need.