Skip to content

Instantly share code, notes, and snippets.

@tiffany-taylor
Created November 1, 2021 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiffany-taylor/023989b9740d4de6c67e1cfe2beee6e1 to your computer and use it in GitHub Desktop.
Save tiffany-taylor/023989b9740d4de6c67e1cfe2beee6e1 to your computer and use it in GitHub Desktop.
<?php
/**
* @param string $sQuery
* @param int $nID
* @return array
*/
public function fetchRows(string $sQuery, int $nID): array
{
// grabbing table name for log messages
preg_match('/FROM\s+(\w+)/', $sQuery, $aMatches);
$this->oLog->info("Attempting to grab entries from table: " . $aMatches[1]);
$oStatement = $this->oDB->prepare($sQuery);
$bFoundResult = $oStatement->execute([$nID]);
if ($bFoundResult === false) {
$this->oLog->info("No entries found from table: " . $aMatches[1] . ", ID: $nID");
return [];
}
return $oStatement->fetchAll(\PDO::FETCH_ASSOC);
}
@Crell
Copy link

Crell commented Nov 1, 2021

   /**
     * @param string $sQuery
     * @param int $nID
     * @return array
     */
    public function fetchRows(string $sQuery, int $nID): Result<array, Err>
    {
        // grabbing table name for log messages
        preg_match('/FROM\s+(\w+)/', $sQuery, $aMatches);
        $this->oLog->info("Attempting to grab entries from table: " . $aMatches[1]);

        $oStatement = $this->oDB->prepare($sQuery);
        $bFoundResult = $oStatement->execute([$nID]);

        if ($bFoundResult === false) {
            $this->oLog->info("No entries found from table: " . $aMatches[1] . ", ID: $nID");
            return new Error(new Err('No entries found'));
        }

        return new Result($oStatement->fetchAll(\PDO::FETCH_ASSOC));
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment