mottwsc Posted October 5, 2009 Share Posted October 5, 2009 I'm trying to vary the connection in a statement based on a variable. Since I could have 100 different connections or more, I don't want to use if-then logic (as below). Instead of: if( x == '001' ) { $result = mysqli_query($cxn001,$sql) } elseif( x == '002' ) { $result = mysqli_query($cxn002,$sql) } What can I use to accomplish this without the if-then construct? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/176582-solved-variable-in-connection-statement/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 5, 2009 Share Posted October 5, 2009 You would use an array, but there is no real good reason for anyone to need more than a few different connections in any one script. Sounds like a bad database table design. Quote Link to comment https://forums.phpfreaks.com/topic/176582-solved-variable-in-connection-statement/#findComment-930910 Share on other sites More sharing options...
Zane Posted October 5, 2009 Share Posted October 5, 2009 $result = mysqli_query(${'cxn' . $x}, $sql); Quote Link to comment https://forums.phpfreaks.com/topic/176582-solved-variable-in-connection-statement/#findComment-930912 Share on other sites More sharing options...
mottwsc Posted October 5, 2009 Author Share Posted October 5, 2009 Thanks, Zanus - that should do it! (for Pfmabismad - the various connections are for different servers. It doesn't involve database design. Thanks, though.) Quote Link to comment https://forums.phpfreaks.com/topic/176582-solved-variable-in-connection-statement/#findComment-930949 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.