src/Mysql.php
public function loadByPrepStmt(
        array $arrWhat = ['*'], 
        array $arrWhere = [], 
        array $arrOrder = ['`id` ASC'], 
        array $arrLimit = []
    ) : array
    {
        $arrStmt = [];
        $strWhere = "";
        if (count($arrWhere) > 0) {
            $arrWhereNames = [];
            foreach ($arrWhere as $key => $val) {
                if (is_array($val) && $val['operator'] === "like") {
                    $arrWhereNames[] = "`".$key."` LIKE :".$key;
                    $arrStmt[':'.$key] = $val['value'];
                } else {
                    $arrWhereNames[] = "`".$key."` = :".$key;
                    $arrStmt[':'.$key] = $val;
                }
            }
            $strWhere = "WHERE ". implode(" AND ", $arrWhereNames) . " ";
        }
        
        $strOrder = "";
        if (count($arrOrder) > 0) {
            $strOrder = "ORDER BY ".implode(", ", $arrOrder) . " ";
        }
        
        $strLimit = "";
        if (isset($arrLimit[0])) {
            $strLimit .= "LIMIT ".((int) $arrLimit[0]);
        }
        if (isset($arrLimit[1])) {
            $strLimit .= "," . ((int) $arrLimit[1]);
        }
        $sql = "SELECT ".implode(",", $arrWhat)." FROM `".$this->tableName."` "
            . $strWhere
            . $strOrder
            . $strLimit;
        
        $stmt = $this->db->prepare($sql);
        $stmt->execute($arrStmt);
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        if (!$result) {
            return [];
        }
        
        return $result;
    }
public function loadByPrepStmt(
        array $arrWhat = ['*'], 
        array $arrWhere = [], 
        array $arrOrder = ['`id` ASC'], 
        array $arrLimit = []
    ) : array
    {
        $arrStmt = [];
        $strWhere = "";
        if (count($arrWhere) > 0) {
            $arrWhereNames = [];
            foreach ($arrWhere as $key => $val) {
                if (is_array($val) && $val['operator'] === "like") {
                    $arrWhereNames[] = "`".$key."` LIKE :".$key;
                    $arrStmt[':'.$key] = $val['value'];
                } else {
                    $arrWhereNames[] = "`".$key."` = :".$key;
                    $arrStmt[':'.$key] = $val;
                }
            }
            $strWhere = "WHERE ". implode(" AND ", $arrWhereNames) . " ";
        }
        
        $strOrder = "";
        if (count($arrOrder) > 0) {
            $strOrder = "ORDER BY ".implode(", ", $arrOrder) . " ";
        }
        
        $strLimit = "";
        if (isset($arrLimit[0])) {
            $strLimit .= "LIMIT ".((int) $arrLimit[0]);
        }
        if (isset($arrLimit[1])) {
            $strLimit .= "," . ((int) $arrLimit[1]);
        }
        $sql = "SELECT ".implode(",", $arrWhat)." FROM `".$this->tableName."` "
            . $strWhere
            . $strOrder
            . $strLimit;
        
        $stmt = $this->db->prepare($sql);
        $stmt->execute($arrStmt);
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        if (!$result) {
            return [];
        }
        
        return $result;
    }
public function loadByPrepStmt ( array $ arrWhat = [ '*' ] , array $ arrWhere = [ ] , array $ arrOrder = [ '`id` ASC' ] , array $ arrLimit = [ ] ) : array { $ arrStmt = [ ] ; $ strWhere = "" ; if ( count ( $ arrWhere ) > 0 ) { $ arrWhereNames = [ ] ; foreach ( $ arrWhere as $ key => $ val ) { if ( is_array ( $ val ) && $ val [ 'operator' ] === "like" ) { $ arrWhereNames [ ] = "`" . $ key . "` LIKE :" . $ key ; $ arrStmt [ ':' . $ key ] = $ val [ 'value' ] ; } else { $ arrWhereNames [ ] = "`" . $ key . "` = :" . $ key ; $ arrStmt [ ':' . $ key ] = $ val ; } } $ strWhere = "WHERE " . implode ( " AND " , $ arrWhereNames ) . " " ; } $ strOrder = "" ; if ( count ( $ arrOrder ) > 0 ) { $ strOrder = "ORDER BY " . implode ( ", " , $ arrOrder ) . " " ; } $ strLimit = "" ; if ( isset ( $ arrLimit [ 0 ] ) ) { $ strLimit .= "LIMIT " . ( ( int ) $ arrLimit [ 0 ] ) ; } if ( isset ( $ arrLimit [ 1 ] ) ) { $ strLimit .= "," . ( ( int ) $ arrLimit [ 1 ] ) ; } $ sql = "SELECT " . implode ( "," , $ arrWhat ) . " FROM `" . $ this -> tableName . "` " . $ strWhere . $ strOrder . $ strLimit ; $ stmt = $ this -> db -> prepare ( $ sql ) ; $ stmt -> execute ( $ arrStmt ) ; $ result = $ stmt -> fetchAll ( PDO :: FETCH_ASSOC ) ; if ( ! $ result ) { return [ ] ; } return $ result ; }
Get Rows from Table using a Prepared Statement
@param array $arrWhat
@param array $arrWhere associative ["name" => "Dave"] OR ["name" => ["operator" => "like", "value" => "Dave"]]
@param array $arrOrder
@param array $arrLimit
@return array
Get Rows from Table using a Prepared Statement