has()

The has() method checks if any row exists that matches the current WHERE conditions. It executes an efficient SELECT 1 query with LIMIT 1 and returns a simple boolean result — true if a match is found, false otherwise.

Signature:

public function has(string $table): bool

Basic Usage

$db->where('email', 'demo@gmail.urx');
$exists = $db->has('test_user');

When to use has()

Use has() when you want to check for existence without loading the actual row. It's more efficient than using get() or getOne() when you only care about "does it exist?".

  • Before inserting a user: check if email or username already exists
  • Check if a specific status or record is already present
  • Protect against duplicates in unique fields

Example: Check for unique email

$db->where('email', 'demo@gmail.urx');
if ($db->has('test_user')) {
  echo "E-Mail already taken.";
} else {
  // safe to insert
}

Performance

Internally, has() executes:

SELECT 1 FROM table WHERE ... LIMIT 1

This makes it ideal for fast existence checks in high-traffic applications.

Security Notes

  • Table names are validated
  • WHERE conditions are passed through secureWhere()
  • Safe to use with all where*() methods

See Also