This is an extension of a piece from late last year titled, "Consolidated query executions." That version had a problem in that it didn't work as advertised with MySQL. As a result it has been re-worked and listed here. It's pending further testing, specifically against SQL Server, it's just listed now in the event that it may prove useful to someone.
The following assumptions are made.
private function run($sql) { $Query = $this->Conn->prepare($sql); if((func_num_args() == 2) && is_array(func_get_arg(1))) { $Query->execute(func_get_arg(1)); } else { $Query->execute(array()); } $errorInfo = $Query->errorInfo(); if($errorInfo[1] != NULL) { return $errorInfo[2]; } $rowCount = $Query->rowCount(); $rowCountStatus = $Query->errorCode(); $fetch = $Query->fetchAll(\PDO::FETCH_ASSOC); $fetchStatus = $Query->errorCode(); if(($rowCountStatus == "00000") && ($fetchStatus == "00000")) { return $fetch; } else { return $rowCount; } }
The following assumptions are made.
- A connection to the database called $this->Conn has already been made by the time this code is called.
- The code references the PDO class and with the notation as it is currently, assumes the use clause as use PDO in the calling code. Otherwise, references will need to be updated to \PDO instead.
Comments
Post a Comment