Jump to content

Recommended Posts

Use two separate queries in that case

 

OK, and one more question.

 

I am trying to choose sql queries in an if statement, but mysql doesn't like it.   Alternatively, I am putting the PDO stuff into a function and passing the query in, but that isn't working either.

 

Is there a way to do this without having to write separate PDO code for each query?

 

I want to do something like this:

try
    {
                 

    if(trueOrfalse)
    {
           $sql = "SELECT * FROM myTable";
        }
        else{
            $sql = "SELECT * FROM someOtherTable";
        }
     
        $stmt = $dbh->prepare($sql);
        $stmt->execute();
        $arrValues = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $row = $stmt->fetch();
        
 
        
    }
    catch(PDOException $e) {
            die($e->getMessage());
    }

But I keep getting a syntax error message.  Apparently sql doesn't like it.

 

Thanks.

Edited by shaung
Is there a way to do this without having to write separate PDO code for each query?

 

 

to reuse code, you would write a general purpose function or class method that accepts the input values, runs the necessary code, and returns the expected result back to the calling program.

 

in those cases where you tried something and got errors, you would need to post the code and the error to get help.

Edited by mac_gyver
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.