Jump to content

Pull columns from 2 tables


shaung

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.