cell (method)

Last updated: Fri, Apr 14, 2023 3:43 pm
Return to Knowledgebase

Purpose

Retrieve a single value from a specific row and column in a database table, using a simplified syntax that allows the table and column names to be specified

Location

users/classes/DB.php

Parameters

# Parameter Data Type Required Description
1 $tablecolumn string Yes A string containing the name of the table and the name of the column to be retrieved, separated by a dot (.) character.
2 $id=[] integer No An optional parameter that can be used to specify the value of the id column to be used in the WHERE clause of the SQL query. If $id is not provided, the method will retrieve the value from the first row in the table.

Returns

Data Type Description of Returned Data
array of objects If the query is successful and returns at least one row, the method retrieves the value of the specified column from the first row of the result set and returns it. If the query fails or returns no rows, the method returns null.

Further Documentation:

The cell() method takes two parameters: $tablecolumn and $id.

$tablecolumn is a string containing the name of the table and the name of the column to be retrieved, separated by a dot (.) character.

$id is an optional parameter that can be used to specify the value of the id column to be used in the WHERE clause of the SQL query. If $id is not provided, the method will retrieve the value from the first row in the table.

The method first splits the $tablecolumn string into two parts: the table name and the column name. If the string cannot be split into two parts, the method returns null.

The method then calls the action() method on the database connection object (which is assumed to have been previously established), passing in an SQL SELECT query that retrieves the specified column from the specified table, with an optional WHERE clause that limits the result to the row(s) with the specified id value (or the first row if no id is provided).


// Assuming $db is an instance of the UserSpice DB class
$user_id = 123;
$email = $db->cell('users.email', ['id', '=', $user_id]);

if ($email !== null) {
echo "User email address: $email";
} else {
echo "No email address found for user ID $user_id";
}

In this example, the $tablecolumn string specifies the email column of the users table. The $id parameter specifies an array that sets the WHERE clause to match the id column with the value of $user_id. The cell() method executes the query and returns the email address value from the specified column of the row with the matching id value. If no matching row is found, the method returns null.