简体   繁体   中英

Prepared statements in Extbase TYPO3 7.6 not working

I want to submit the query as a prepared statement, like below.

$query = $this->createQuery();
$query->getQuerySettings()->usePreparedStatement(TRUE);
$sqlParamList[] = 'test@gamil.com';
$sql = 'SELECT uid FROM table_name WHERE email = ?';
$query->statement($sql, $sqlParamList);
$result = $query->execute();

But I always get errors like below.

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?' at line 1'

Where I am wrong?

You need to parse your $sql to a prepared statement first:

$preparedSql = $this->objectManager->get(\TYPO3\CMS\Core\Database\PreparedStatement::class, $sql, 'table_name');

With $this->objectManager->get() you instantiiate the class PreparedStatement with the arguments $sql and 'table_name' .

This will change your $sql and parse the ? to be used as prepared statement.

Another approach:

$query = $this->createQuery();
$query->getQuerySettings()->usePreparedStatement(TRUE);
$sqlParamList = [
    ':email' => 'test@gamil.com'
];
$sql = 'SELECT uid FROM table_name WHERE email = :email';
$query->statement($sql, $sqlParamList);
$result = $query->execute();

Cannot test it, just an approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM