Skip to content
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

Open
jdaily opened this issue Sep 25, 2013 · 3 comments
Open

Dynamically add placeholders? #18

jdaily opened this issue Sep 25, 2013 · 3 comments

Comments

@jdaily
Copy link

jdaily commented Sep 25, 2013

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)";

    if ($Search == false) {
        $Query.=" AND b.ID =?i";
    } else {
        $Query.=" ...."
    }

}

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?

@colshrapnel
Copy link
Owner

It is indeed a shame on me. I didn't make it possible for the array with parameters.
I am already aware of the problem and planning for the new version.

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 call_user_func_array() but I doubt it worth the trouble.
Yet I can write the code if you want.

@stephancasas
Copy link

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.

@00dev
Copy link
Contributor

00dev commented Dec 21, 2019

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
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants