Fetch data from an sql query
Returns always an array of results

@param string			$query
@param array 			$params
@return array
13185
public static function fetch( $query, $params = array(), $handler = null, $arguments = array( 'obj' ) ) 
	{
		return Handler::create( $handler )->fetch( $query, $params, $arguments );
	}
186010
public function fetchOne($query, $parameters = array())
    {
        $result = $this->fetch($query, $parameters);
        return is_array($result) && !empty($result) ? reset($result) : false;
    }
738586
protected function prepare(string $query, array $params): array
	{
		// Prepare query and parameters

		[$query, $params] = $this->prepareQueryAndParams($query, $params);

		// Create prepared statement

		try
		{
			prepare:

			$statement = $this->pdo->prepare($query);
		}
		catch(PDOException $e)
		{
			if($this->isConnectionLostAndShouldItBeReestablished())
			{
				$this->reconnect();

				goto prepare;
			}

			throw new PDOException($e->getMessage() . ' [ ' . $this->prepareQueryForLog($query, $params) . ' ] ', (int) $e->getCode(), $e->getPrevious());
		}

		// Bind parameters

		foreach($params as $key => $value)
		{
			$this->bindParameter($statement, $key, $value);
		}

		// Return query, parameters and the prepared statement

		return ['query' => $query, 'params' => $params, 'statement' => $statement];
	}
612525
public function query($query, $params = []) {
		
		if(false === is_string($query)) {
			return trigger_error(sprintf('Argument 1 passed to %s() must be of the type string, "%s" given', __METHOD__, gettype($query)), E_USER_ERROR);
		}

		if(null !== $params && false === is_array($params)) {
			return trigger_error(sprintf('Argument 2 passed to %s() must be of the type array, "%s" given', __METHOD__, gettype($params)), E_USER_ERROR);
		}

		if(null === $this -> connection) {
			$this -> connect();
		}

		$bind = $this -> parse($params, $query);

		//Write time for debugging
		if(null === $this -> trace) {
			$this -> trace = microtime(true);	
		}
		
		//Prepare statement
		$stmt = $this -> connection -> prepare($query);

		if(false === $stmt) {
			return trigger_error($this -> connection  -> error, E_USER_ERROR);
		}

		if(count($bind) > 0) {

			if(false === ($stmt -> param_count == count($bind) - 1)) {
				return trigger_error('Number of variable elements query string does not match number of bind variables');
			}

			call_user_func_array([$stmt, 'bind_param'], $bind);
		}

		$this -> stmt[] = $stmt;
		$this -> shadow = $this -> stmt;

		return $this;
	}
334486
public function query($query, $params = []) {
		$statement = $this->get_statement($query, $params);
		$statement->execute();
	}
1035981
public function column($query = '', $params = null)
    {
        $query = trim($query);
        if (empty($query)) {
            $query = $this->build();
            if (!$params) {
                $params = $this->getBindValues();
            }
        }

        $this->resetAll();
        $this->lastSql = $query;

        $this->execute($query, $params);
        $columns = $this->sQuery->fetchAll(PDO::FETCH_NUM);
        $column  = null;
        foreach ($columns as $cells) {
            $column[] = $cells[0];
        }
        return $column;
    }
325531
public function query($sql, $params = [], $options = [])
    {
        $conn = $this->getConn(!empty($options['forceMaster']) ? true : false);

        $stmt = $this->prepare($conn, $sql, $params);

        $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
        if ($rows === false) {
            $this->throwException();
        }
        if (!empty($options['getRow'])) {
            $rows = isset($rows[0]) ? $rows[0] : null;
        }
        return $rows;
    }
1042675
public function multiquery($query, $params = []) {

		if(true === is_array($query)) {
			$query = implode(";\n", $query);
		}

		if(false === is_string($query)) {
			return trigger_error(sprintf('Argument 1 passed to %s() must be of the type string or array, "%s" given', __METHOD__, gettype($query)), E_USER_ERROR);
		}

		$results = new ResultSet([], 'array');

		if(null === $this -> connection) {
			$this -> connect();
		}

		//Escape paramaters
		$query = $this -> escapeSql($query, $params);

		if($this -> connection -> multi_query($query)) {

			$array = [];

			do {

				if($result = $this -> connection -> use_result()) {

					while($row = $result -> fetch_array(MYSQLI_ASSOC)) {
						$array[] = $row;
					}

					$result -> close();
					$results -> append($array);
					$array = [];
				}
			}

			while($this -> connection -> more_results() && $this -> connection -> next_result());
		}

		return $results;
	}
792087
public function get($sql, $params = array(), $style = PDO::FETCH_ASSOC) {
		$statement = $this->query($sql, $params);

		$results = [];

		while ($row = $statement->fetch($style)) {
			$results[] = $row;
		}

		return $results;
	}
1080838
final public static function getObjectsByQuery($query, $params=[]) {
		
		$app	= Application::getInstance();
		$db		= Database::getInstance();
		$class	= get_called_class();

		// run query
		$db->setQuery($query);
		$list = $db->loadObjectList($params);
		
		// array that returns and custom binds
		$objects = [];
		$customBinds = [];
		
		if (is_array($list) and isset($list[0])) {
			
			$binds = $class::getBinds();
			
			// get object properties from query
			$fields  = get_object_vars($list[0]);

			// search for custom field names
			foreach ($fields as $field=>$value) {
				if (!array_search($field, $binds)) {
					$customBinds[Utilities::getCamelCase($field)] = $field;
				}
			}

			// build each object
			foreach ($list as $row) {
				
				$object = new $class($row);

				// populate custom properties
				foreach ($customBinds as $customProp=>$customField) {
					$object->$customProp = $row->$customField;
				}
				
				// turn on loaded-from-db flag
				$object->loadedFromDb = TRUE;

				$objects[] = $object;
				
			}
			
		}
		
		$app->logEvent('Loaded ' . count($objects) . ' ' . $class . ' objects with custom fields ' . implode(',', $customBinds));
		
		return $objects;
		
	}
609752
public function executeQuery($query, array $params = []) {
        $this->connect();

        $cacheKey = null;
        $cacheLength = null;

        // Determine cache key and lengths
        if ($query instanceof Query) {
            $cacheKey = $query->getCacheKey();
            $cacheLength = $query->getCacheLength();

        } else if (!is_string($query)) {
            throw new InvalidQueryException('Query must be a raw SQL string or a Titon\Db\Query instance');
        }

        // Execute and return result
        return $this->_result = $this->cacheQuery($cacheKey, function(AbstractPdoDriver $driver) use ($query, $params) {
            $isQuery = ($query instanceof Query);

            // Prepare statement and bind parameters
            if ($isQuery) {
                $statement = $driver->buildStatement($query);
                $binds = $driver->resolveParams($query);

            } else {
                $statement = $driver->getConnection()->prepare($query);
                $binds = [];

                foreach ($params as $value) {
                    $binds[] = [$value, $driver->resolveType($value)];
                }
            }

            foreach ($binds as $i => $value) {
                $statement->bindValue($i + 1, $value[0], $value[1]);
            }

            $statement->params = $binds;

            // Gather and log result
            if ($isQuery) {
                $result = new PdoResultSet($statement, $query);
            } else {
                $result = new PdoResultSet($statement);
            }

            $driver->logQuery($result);

            return $result;
        }, $cacheLength);
    }
1274378
public function query($query, $params = array())
  {
    $results = $this->api->get($query, $params);
    if(!isset($results->errors)) {
      return $results;
    } else {
      return $this->debug_mode ? $results->errors[0]->message : null;
    }
  }
1173039
public function query($query, array $params = [])
    {
        $this->lastStatement = $this->getPDO()->prepare($query);
        $this->lastStatement->execute($this->prepareParams($params));
        return $this->lastStatement;
    }
750867
public function fetchOne(string $sql, array $params = [])
    {
        $result = $this->query($sql, $params);

        if ($result->count() > 1) {
            throw new QueryException(
                $this,
                $this->getFinalQuery($sql, $params),
                sprintf('Fetch incomplete resultset, %s remaining', $result->count() - 1)
            );
        }

        if ($result->valid()) {
            return $result->current();
        } else {
            return false;
        }
    }
976018
public function queryAndReturnResult($query, $params = [])
    {
        $schema = $this->schema();
        $context = $this->getContext();

        $last = function ($schema, $query, $context, $params) {
            return GraphQL::executeQuery($schema, $query, null, $context, $params);
        };

        return $this->callMiddleware($schema, $query, $context, $params, $last);
    }
259960
protected function paramArrays(&$query, &$params)
    {
        if (!is_array($params)) {
            return;
        }

        $newParams = [];

        $this->modifyQuery($query, function ($part) use (&$params, &$newParams) {
            $newPart = "";
            $tmpPart = $part;

            while (count($params) > 0) {
                $pos = strpos($tmpPart, "?");
                if ($pos === false) {
                    break;
                }

                $val = array_shift($params);

                $newPart .= substr($tmpPart, 0, $pos);
                $tmpPart = substr($tmpPart, $pos + 1);

                # If this is just a straight value then don't do anything to it
                if (!is_array($val)) {
                    $newPart .= "?";
                    $newParams[] = $val;
                    continue;
                }

                # If the array is only 1 element (or no elements) long then convert it to an = (or <> for NOT IN)
                if (count($val) < 2) {
                    $newPart = preg_replace("/\s*\bNOT\s+IN\s*$/i", "<>", $newPart);
                    $newPart = preg_replace("/\s*\bIN\s*$/i", "=", $newPart);
                    $newPart .= "?";
                    $newParams[] = reset($val);
                    continue;
                }

                # Convert each element of the array to a separate marker
                $markers = [];
                foreach ($val as $v) {
                    $markers[] = "?";
                    $newParams[] = $v;
                }
                $newPart .= "(" . implode(",", $markers) . ")";
            }

            $newPart .= $tmpPart;

            return $newPart;
        });

        $params = $newParams;
    }
1321718
public function getAll($sql, $params = array())
    {
        $stmt = $this->execute($sql, $params);
        return $stmt->fetchAll($this->fetchMode);
    }