-
Notifications
You must be signed in to change notification settings - Fork 197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamically add placeholders? #18
Comments
It is indeed a shame on me. I didn't make it possible for the array with parameters. At the moment the most simple workaround is to repeat the call right after query definition: if ($Search == false) {
$Query.=" AND b.ID =?i";
$result = $dbobj->getAll($Query, $ID, $Number);
} else {
$Query.=" ...."
$result = $dbobj->getAll($Query, $ID);
} There is also a method involving |
When my number of parameters vary, I find it useful to simply include additional parameter chars inside of or after comment chars in the query. Not sure if anyone's still watching this six-year-old thread, but doing this made short work of my similar issue. |
I prefer not to add placeholders and avoid to build query with concatenation. When I want add condition (like on above example) I do like below $additionalCondition = '';
if ($Number !== NULL) {
$additionalCondition = $dbobj->parse(
" AND b.Number = ?i",
$Number
);
}
$result = $dbobj->getAll(
"SELECT *
FROM Numbers b
WHERE b.ID = ?i AND (b.Active = 1 OR b.Active is NULL) ?p",
$ID,
$additionalCondition
); |
My queries change depending on what php parameters I have, the simplest example I have is:
if ($Number!= NULL && $ID != NULL) {
$Query = "SELECT *
FROM Numbers b
WHERE b.ID =?i AND (b.Active=1 OR b.Active is NULL) AND b.Number=?i;
} else if ($Number == NULL && $ID != NULL) {
$Query = "SELECT *
FROM Numbers b
WHERE 1 AND (b.Active=1 OR b.Active is NULL)";
}
which would give me a final operation of:
$result = $dbobj->getAll($Query, $ID, $Number);
or
$result = $dbobj->getAll($Query, $ID);
The issue I'm facing is that in the final query the number of placeholders will vary. Is there some way I can build the placeholders as I build the query and pass them in as an array? Also not sure if the way I'm building my queries is unorthodox thus causing my issue with prepared statements.
Any Advice?
The text was updated successfully, but these errors were encountered: