shaung Posted December 24, 2013 Share Posted December 24, 2013 I want to pull two columns, one from each table, in one sql statement. They have no relation to each other. It is to populate two dropdown lists. How can I make this happen? I keep getting errors. Thanks Link to comment https://forums.phpfreaks.com/topic/284919-pull-columns-from-2-tables/ Share on other sites More sharing options...
Barand Posted December 24, 2013 Share Posted December 24, 2013 Use two separate queries in that case Link to comment https://forums.phpfreaks.com/topic/284919-pull-columns-from-2-tables/#findComment-1463063 Share on other sites More sharing options...
shaung Posted December 24, 2013 Author Share Posted December 24, 2013 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. Link to comment https://forums.phpfreaks.com/topic/284919-pull-columns-from-2-tables/#findComment-1463064 Share on other sites More sharing options...
mac_gyver Posted December 25, 2013 Share Posted December 25, 2013 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. Link to comment https://forums.phpfreaks.com/topic/284919-pull-columns-from-2-tables/#findComment-1463071 Share on other sites More sharing options...
fenway Posted December 28, 2013 Share Posted December 28, 2013 Assuming it truly goes into the "same dropdown", you might get away with some version of UNION. Link to comment https://forums.phpfreaks.com/topic/284919-pull-columns-from-2-tables/#findComment-1463150 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.