Search code examples
phpfetchoci8

I am trying to return an array of User objects but my variable $database->FetchArray() return false


My program does not execute the foreach because $database->FetchArray() is false.

However my request is correct and executed ($success is equal to true).

public static function FindBasicRecipients(): array|bool
    {
        $database = new Database('log', 'pwd', 'desc');
        $database->Connect();
        $database->Parse("SELECT 
            ID_WORKSPACE_USERS,
            LOGIN_WORKSPACE_USERS
            FROM WORKSPACE_USERS
            WHERE FONCTION_WORKSPACE_USERS IN ('Job 1','Job 2', 'Job 3')
            ORDER BY EMAIL_WORKSPACE_USERS"
        );
        $success = $database->Execute();
        if ($success) {
            $basicRecipients = [];
            foreach ($database->FetchArray() as $row) {
                $basicRecipients[] = new User(
                    $row['ID_WORKSPACE_USERS'],
                    $row['LOGIN_WORKSPACE_USERS']
                );
            }
            $database->FreeStatement();
            $database->Close();
            return $basicRecipients;
        } else {
            $database->FreeStatement();
            $database->Close();
            return false;
        }
    }

Here is my Database class with my FetchArray method :

public function FetchArray($mode = null): array|false
    {
        return oci_fetch_array($this->statement, $mode);
    }

Solution

  • oci_fetch_array() just returns one row of results. If it returns anything, You'll loop over the columns of the first row, not all the results. It will return false if there are no results or you've already fetched all the results.

    To loop over all the results, you should use

    while ($row = $database->FetchArray())