Contao\Database\Statement::prepare PHP Method

prepare() public method

Prepare a query string so the following functions can handle it
public prepare ( string $strQuery ) : Statement
$strQuery string The query string
return Statement The statement object
    public function prepare($strQuery)
    {
        if ($strQuery == '') {
            throw new \Exception('Empty query string');
        }
        $this->strQuery = trim($strQuery);
        // Auto-generate the SET/VALUES subpart
        if (strncasecmp($this->strQuery, 'INSERT', 6) === 0 || strncasecmp($this->strQuery, 'UPDATE', 6) === 0) {
            $this->strQuery = str_replace('%s', '%p', $this->strQuery);
        }
        // Replace wildcards
        $arrChunks = preg_split("/('[^']*')/", $this->strQuery, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        foreach ($arrChunks as $k => $v) {
            if (substr($v, 0, 1) == "'") {
                continue;
            }
            $arrChunks[$k] = str_replace('?', '%s', $v);
        }
        $this->strQuery = implode('', $arrChunks);
        return $this;
    }

Usage Example

Beispiel #1
0
 /**
  * Prepare a query and return a Database\Statement object
  *
  * @param string $strQuery The query string
  *
  * @return Database\Statement The Database\Statement object
  */
 public function prepare($strQuery)
 {
     $objStatement = new \Database\Statement($this->resConnection, $this->blnDisableAutocommit);
     return $objStatement->prepare($strQuery);
 }