WPDD

WordPress Develop & Design

custom get row

    // Retrieve a single row
    public function get_row($where) {
        $sql = $this->wpdb->prepare(
            "SELECT * FROM {$this->table} WHERE " . key($where) . " = %s",
            array_values($where)
        );
    
        $result = $this->wpdb->get_row($sql, ARRAY_A);
    
        return $result;
    }

Explanation of the get_row() Function

This function retrieves a single row from the database based on a given condition.
Step-by-Step Breakdown

public function get_row($where) {

This function takes one parameter → $where, which is an associative array like:

[‘lesson_id’ => 5]

This means: “Get the lesson where lesson_id = 5”.

$sql = $this->wpdb->prepare(
“SELECT * FROM {$this->table} WHERE ” . key($where) . ” = %s”,
array_values($where)
);

Builds the SQL Query Dynamically
{$this->table} → Inserts the table name dynamically.
key($where) → Extracts the column name (lesson_id in this example).
= %s → Uses %s as a placeholder for the value (5 in this case).
array_values($where) → Gets the value (5) and replaces %s.

📌 Final Query Example (if $where = [‘lesson_id’ => 5]):

SELECT * FROM wp_wpca_lessons WHERE lesson_id = ‘5’

👉 Secure: Uses prepare() to prevent SQL injection.

$result = $this->wpdb->get_row($sql, ARRAY_A);

Executes the SQL query.
Retrieves a single row (if found).
Returns it as an associative array (ARRAY_A) like:

[
‘lesson_id’ => 5,
‘session_id’ => 1,
‘class_id’ => 2,
‘date’ => ‘2025-03-01’,
‘time’ => ’15:00:00′
]

return $result;

Returns the retrieved row.
If no row is found, it returns null.

Example Usage

$lesson = new WPCA_Lesson();
$retrieved_lesson = $lesson->get_row([‘lesson_id’ => 5]);

if ($retrieved_lesson) {
print_r($retrieved_lesson);
} else {
echo “Lesson not found.”;
}

👉 Expected Output (if lesson_id = 5 exists):

Array
(
[lesson_id] => 5
[session_id] => 1
[class_id] => 2
[date] => 2025-03-01
[time] => 15:00:00
)

Summary

✅ Dynamically builds SQL queries based on the $where condition.
✅ Uses prepare() to prevent SQL injection.
✅ Fetches a single row from the database.
✅ Returns an associative array (ARRAY_A) or null if no match is found.
Would You Like to Modify It?

Add error handling if no data is found?
Allow fetching by multiple conditions (e.g., lesson_id = 5 AND session_id = 2)?


Let me know! 🚀
You said:

ARRAY_A
is this saved word or i can write what i like?
ChatGPT said:

ARRAY_A is not a reserved word in PHP or MySQL. It is a WordPress-specific constant used with wpdb functions to specify how the result should be returned.
Can You Change It?

✅ Yes, but only to other formats supported by wpdb->get_row().
Available Options for wpdb->get_row()

When retrieving a row, you can specify how the result should be returned:
ModeDescription Example Output:
ARRAY_A – Returns an associative array [‘lesson_id’ => 5, ‘session_id’ => 1]
ARRAY_N – Returns a numeric array [5, 1] (indexed numerically)
OBJECT – Returns an object $row->lesson_id

Example Usage

// Get result as an associative array
$result = $this->wpdb->get_row($sql, ARRAY_A);

// Get result as a numeric array
$result = $this->wpdb->get_row($sql, ARRAY_N);

// Get result as an object
$result = $this->wpdb->get_row($sql, OBJECT);