usernameExists (function)

Last updated: Fri, May 26, 2023 9:35 am
Return to Knowledgebase

Purpose

The usernameExists function in UserSpice is a custom function used to check if a username already exists in the users table of the database.

Location

users/helpers/us_helpers.php

Parameters

# Parameter Data Type Required Description
1 $username string Yes The username to check for existence.

Returns

Data Type Description of Returned Data
array If the username exists in the users table, the function returns an array containing user records. If the username doesn't exist, the function returns null.

Further Documentation:

$username = 'john_doe';

// Check if the username exists
$userRecords = usernameExists($username);

if ($userRecords) {
echo "Username '$username' already exists.";
// Perform appropriate action
} else {
echo "Username '$username' does not exist.";
// Perform appropriate action
}



In the example above, the usernameExists function is called with the $username variable as the argument. The function queries the users table to check if any user records match the provided username. If a match is found, the function returns an array of user records. If no match is found, it returns null.

You can capture the return value of the function in a variable, such as $userRecords, and then use conditional statements to perform the desired actions based on the existence or non-existence of the username.

In the example, if the $userRecords variable is truthy (i.e., the username exists), it prints a message indicating that the username already exists. Otherwise, it prints a message indicating that the username does not exist. You can customize the actions according to your specific requirements.