I don't understand, why my browser return this error and why my sql doesn't work. I try resolved this problem, but I don't know, how can I do this.
private function getFromDb()
{
$queryBuilder = $this->connection->createQueryBuilder();
$query = $queryBuilder
->select('art1.articleID', 'art1.valueID')
->from('s_filter_articles', 'art1')
->innerJoin('art1', 's_filter_articles', 'art2', 'art1.articleID = art2.articleID')
->where('art1.valueID = 12499')
->andWhere('art2.valueID = 12500');
return $query->execute()->fetchAll();
}
This code return this array:
array:2 [▼
0 => array:2 [▼
"articleID" => "2225"
"valueID" => "12499"
]
1 => array:2 [▼
"articleID" => "2250"
"valueID" => "12499"
]
]
...and I would like to delete ALL this result in MySQL. SO I wrote this code:
public function removeFromDb()
{
$result = $this->getFromDb();
foreach ($result as $option) {
$queryBuilder = $this->connection->createQueryBuilder();
$query = $queryBuilder
->delete('s_filter_articles', 's')
->where('articleID = :articleID')
->andWhere('valueID = :valueID')
->setParameter(':articleID', $option['articleID'])
->setParameter(':valueID', $option['valueID']);
return $query->execute();
}
}
BUT my browser return this error:
> Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or
> access violation: 1064 You have an error in your SQL syntax; check the
> manual that corresponds to your MySQL server version for the right
> syntax to use near 's WHERE (articleID = '2225') AND (valueID =
> '12499')' at line 1 in
> /var/www/html/strefatenisa.local/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php
> on line 131
> Doctrine\DBAL\DBALException: An exception occurred while executing
> 'DELETE FROM s_filter_articles s WHERE (articleID = ?) AND (valueID =
> ?)' with params ["2225", "12499"]: SQLSTATE[42000]: Syntax error or
> access violation: 1064 You have an error in your SQL syntax; check the
> manual that corresponds to your MySQL server version for the right
> syntax to use near 's WHERE (articleID = '2225') AND (valueID =
> '12499')' at line 1 in
> /var/www/html/strefatenisa.local/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php
> on line 131
EDIT: If I do normal query in my mysql, all works good.
SELECT art1.articleID, art1.valueID
FROM s_filter_articles as art1
INNER JOIN s_filter_articles as art2 ON art1.articleID = art2.articleID
WHERE art1.valueID = 12499
AND art2.valueID = 12500
DELETE FROM `s_filter_articles` WHERE `articleID` = 2225 AND `valueID` = 12499
Update your function removeFromDb() with this code:
public function removeFromDb()
{
$result = $this->getFromDb();
foreach($result as $option){
$queryBuilder = $this->connection->createQueryBuilder();
$queryBuilder->delete('s_filter_articles', 's')
->where('articleID = :articleID')
->andWhere('valueID = :valueID')
->setParameter(':articleID', $option['articleID'])
->setParameter(':valueID', $option['valueID']);
$query = $queryBuilder->getQuery();
$query->execute();
}
}
createQueryBuilder function returns a new instance of the QueryBuilder class which doesn't have any declaration of the execute() method. That's why I added the getQuery() method to return the Query class which is the one that has the execute() method. Another thing that I removed is the return from inside of the foreach. I hope this work for you, if not let me know and I can help you.
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.