PDOWrapper::select PHP Method

select() public method

- retrieve information from the database, as an array
public select ( string $table, array $params = null, integer $limit = null, integer $start = null, array $order_by = null, boolean $use_master = false ) : mixed
$table string - the name of the db table we are retreiving the rows from
$params array - associative array representing the WHERE clause filters
$limit integer (optional) - the amount of rows to return
$start integer (optional) - the row to start on, indexed by zero
$order_by array (optional) - an array with order by clause
$use_master boolean (optional) - use the master db for this read
return mixed - associate representing the fetched table row, false on failure
    public function select($table, $params = null, $limit = null, $start = null, $order_by = null, $use_master = false)
    {
        // building query string
        $sql_str = "SELECT * FROM {$table}";
        // append WHERE if necessary
        $sql_str .= count($params) > 0 ? ' WHERE ' : '';
        $add_and = false;
        // add each clause using parameter array
        if (empty($params)) {
            $params = array();
        }
        foreach ($params as $key => $val) {
            // only add AND after the first clause item has been appended
            if ($add_and) {
                $sql_str .= ' AND ';
            } else {
                $add_and = true;
            }
            // append clause item
            $sql_str .= "{$key} = :{$key}";
        }
        // add the order by clause if we have one
        if (!empty($order_by)) {
            $sql_str .= ' ORDER BY';
            $add_comma = false;
            foreach ($order_by as $column => $order) {
                if ($add_comma) {
                    $sql_str .= ', ';
                } else {
                    $add_comma = true;
                }
                $sql_str .= " {$column} {$order}";
            }
        }
        // now we attempt to retrieve the row using the sql string
        try {
            // decide which database we are selecting from
            $pdo_connection = $use_master ? $this->getMaster() : $this->getSlave();
            $pdoDriver = $pdo_connection->getAttribute(PDO::ATTR_DRIVER_NAME);
            //@TODO MS SQL Server & Oracle handle LIMITs differently, for now its disabled but we should address it later.
            $disableLimit = array("sqlsrv", "mssql", "oci");
            // add the limit clause if we have one
            if (!is_null($limit) && !in_array($pdoDriver, $disableLimit)) {
                $sql_str .= ' LIMIT ' . (!is_null($start) ? "{$start}, " : '') . "{$limit}";
            }
            $pstmt = $pdo_connection->prepare($sql_str);
            // bind each parameter in the array
            foreach ($params as $key => $val) {
                $pstmt->bindValue(':' . $key, $val);
            }
            $pstmt->execute();
            // now return the results, depending on if we want all or first row only
            if (!is_null($limit) && $limit == 1) {
                return $pstmt->fetch(PDO::FETCH_ASSOC);
            } else {
                return $pstmt->fetchAll(PDO::FETCH_ASSOC);
            }
        } catch (PDOException $e) {
            if (self::$LOG_ERRORS == true) {
                error_log('DATABASE WRAPPER::' . print_r($e, true));
            }
            $this->pdo_exception = $e;
            return false;
        } catch (Exception $e) {
            if (self::$LOG_ERRORS == true) {
                error_log('DATABASE WRAPPER::' . print_r($e, true));
            }
            $this->pdo_exception = $e;
            return false;
        }
    }