PDOWrapper::query PHP Method

query() public method

- returns data from a free form select query
public query ( string $query, array $params = [], boolean $use_master = false ) : mixed
$query string - the SQL query we are executing
$params array - a list of bind parameters
$use_master boolean (Optional) - whether or not to use the master connection
return mixed - the affected rows, false on failure
    public function query($query, $params = array(), $use_master = false)
    {
        try {
            // decide which database we are selecting from
            $pdo_connection = $use_master ? $this->getMaster() : $this->getSlave();
            $pstmt = $pdo_connection->prepare($query);
            // bind each parameter in the array
            foreach ((array) $params as $key => $val) {
                $pstmt->bindValue($key, $val);
            }
            // execute the query
            $pstmt->execute();
            // now return the results
            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;
        }
    }

Usage Example

 /**
  * Static method for creating PDOWrapper object with MYSQL connection
  *
  * @param   string  $host           Mysql host
  * @param   string  $dbName         Mysql database name
  * @param   string  $username       Mysql user name
  * @param   string  $password       Mysql password
  * @param   string  $charset        Connection charset
  * @return PDOWrapper
  */
 public static function openMysql($host, $dbName, $username, $password, $charset = "")
 {
     $wrapper = new PDOWrapper("mysql:host={$host};dbname={$dbName}", $username, $password);
     if ($charset && !$wrapper->getLastError()) {
         $wrapper->query("SET NAMES ?", array($charset));
     }
     return $wrapper;
 }
All Usage Examples Of PDOWrapper::query