Basecoat\DB::select PHP Method

select() public method

Run a SELECT query with optional data binding values
public select ( string $query, array $bindings = null, boolean $useMaster = false, boolean $fetchAll = false ) : integer
$query string SQL Select query to run
$bindings array array of values to bind to the query. Can be an associative array to bind by name
$useMaster boolean set to TRUE to use the master server connection
$fetchAll boolean 1 to fetch data into an associated array ($this->selectResult), 0 to just run the query (use on of the fetch/fetchAll functions to retrieve data)
return integer returns number of rows selected. Use SQL_CALC_FOUND_ROWS with qFoundRows if using LIMIT, to get total rows found.
    public function select($query, $bindings = null, $useMaster = false, $fetchAll = false)
    {
        static $retries = 0;
        if ($this->connect($useMaster) == -1) {
            return -2;
        }
        $debug = null;
        $sth = null;
        if ($useMaster) {
            $dbh =& self::$mdbh;
            if (is_object(self::$msth)) {
                self::$msth->closeCursor();
            }
            self::$msth =& $sth;
            $connectionLabel = self::$mConnectionLabel;
        } else {
            $dbh =& $this->dbh;
            if (is_object($this->sth)) {
                $this->sth->closeCursor();
            }
            $this->sth =& $sth;
            $connectionLabel = $this->connectionLabel;
        }
        // Clear result holder
        $this->selectResult = array();
        $this->lastQuery = $query;
        if (is_null($bindings)) {
            $qStartTime = round(microtime(true), 3);
            $sth = $dbh->query($query);
            $qTime = round(microtime(true), 3) - $qStartTime;
            if ($sth === false) {
                $this->logErrorInfo($dbh, $connectionLabel);
                $debug = $this->errorMsg;
                // Check for dropped connection
                if (in_array($this->errorCode, self::$reconnect_on_error) && $retries == 0) {
                    $retries++;
                    $this->connect($useMaster, true);
                    $result = $this->select($query, $bindings, $useMaster, $fetchAll);
                } else {
                    $result = -1;
                }
            } else {
                $result = $sth->rowCount();
                if ($retries > 0) {
                    $retries = 0;
                }
                // Check if we need to run an explain for debugging
                if ($this->debug > 0) {
                    $debug = $this->explainQuery($query, null, $dbh);
                }
            }
            $this->updateProfiling($query, $bindings, $qTime, $result, $connectionLabel, $debug);
        } else {
            $presult = $this->prepare($query, $useMaster);
            if ($presult > 0) {
                $result = $this->execute($bindings, $useMaster);
                // Check if we need to run an explain for debugging
                if ($this->debug > 0) {
                    $debug = $this->explainQuery($query, $bindings, $dbh);
                }
            }
        }
        if ($result > -1) {
            if ($fetchAll) {
                if ($this->fetchAll($this->selectResult, null, false, $useMaster) == -1) {
                    return -1;
                }
            }
            return $result;
        } else {
            return $result;
        }
    }