Create a simple prepared statement with a parameter, which is used more than once, and bind it to a variable. The following code example doesn't make sense but shows the problem:
$db = \Joomla\CMS\Factory::getContainer()->get('DatabaseDriver');
$someValue = 2;
$query = $db->getQuery(true)
->select('*')
->from('#__action_logs')
->where('id < :someValue AND user_id < :someValue ;')
->bind(':someValue', $someValue);
$db->setQuery($query);
$db->execute();
All occurences of the parameter (:someValue) should be replaced and the query should be executed successfully.
Exception: The number of variables must match the number of parameters in the prepared statement
Joomla 4.4.9
The error occurs using Joomla 4.4.9 but seems to be fixed in Joomla 5.2.1. Of course, there is an easy workaround (using different placeholders) but it took me a while to understand the problem (my original Sql statement was more complicated), so I think its worth to fix it to save others from having the same problem.
Labels |
Removed:
?
|
Labels |
Added:
No Code Attached Yet
|
Title |
|
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2024-11-26 19:06:07 |
Closed_By | ⇒ | Quy |
@Quy Thank you for the quick response! Yes, I read the docs and saw this, but did not find any hint mentioning, that a parameter is not allowed to be used more than once. Even weirder that there is a different behaviour in different versions of joomla (4.4.9 / 5.2.1) without any documentation.
@mariograf Can you update the docs?
It's possible to use one variable for all bind values and ParameterTypes.
See example in manual: https://manual.joomla.org/docs/general-concepts/database/select-data/