mottwsc Posted September 21, 2010 Share Posted September 21, 2010 I have multiple servers and I'm trying to have a user connect to the right server based on their 'assigned' server. So, I need to use different variable names based on that in an include file earlier in the main script, I have... $hostSignIn0001="server1"; $userSignIn0001="xxx1"; $passwordSignIn0001="yyy1"; $dbnameSignIn0001="zzz1"; $hostSignIn0001="server2"; $userSignIn0001="xxx2"; $passwordSignIn0001="yyy2"; $dbnameSignIn0001="zzz2"; in the main script, I have a variable that has the assigned server and I want to use the right connection variables based on this. I could use eval to turn $hostSignIn into $hostSignIn0001 (and so forth for the other variables) and then it would pull the right info, but I'm looking for an alternative way to do this since I'd like to exclude eval from functioning in my scripts using php.ini. Is there an alternative way to do this without using eval? if (!$cxnSignIn = mysqli_connect($hostSignIn, $userSignIn, $passwordSignIn, $dbnameSignIn)) { exit; } Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/214033-use-variable-connection-strings-without-eval/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 21, 2010 Share Posted September 21, 2010 You would either use an array or a switch/case statement. You would never use eval() to determine values and variables to use. It is for when you need to evaluate php code that is in a string (such as for a template) and nothing you have shown in this post involves more than simple array indexing or simple conditional logic. Quote Link to comment https://forums.phpfreaks.com/topic/214033-use-variable-connection-strings-without-eval/#findComment-1113842 Share on other sites More sharing options...
rwwd Posted September 21, 2010 Share Posted September 21, 2010 You could use one function and just have a ternary operator inside the function for each paramter and just switch them depending on the state of a constant; I have this method set, and apart from slight memory usage, and setting up a constant to use as a switch it functions fine, just a suggestion though Rw Quote Link to comment https://forums.phpfreaks.com/topic/214033-use-variable-connection-strings-without-eval/#findComment-1113846 Share on other sites More sharing options...
mottwsc Posted September 21, 2010 Author Share Posted September 21, 2010 Thanks to both of you for your suggestions. It looks like the array is probably the way to go since I'll be adding servers periodically in a table and the array can be loaded from that table. Quote Link to comment https://forums.phpfreaks.com/topic/214033-use-variable-connection-strings-without-eval/#findComment-1113901 Share on other sites More sharing options...
sasa Posted September 22, 2010 Share Posted September 22, 2010 are you try this <?php $hostSignIn0001="server1"; $userSignIn0001="xxx1"; $passwordSignIn0001="yyy1"; $dbnameSignIn0001="zzz1"; $hostSignIn0002="server2"; $userSignIn0002="xxx2"; $passwordSignIn0002="yyy2"; $dbnameSignIn0002="zzz2"; $site = 2; $site = str_pad($site, 4, '0', STR_PAD_LEFT); $host = ${'hostSignIn'.$site}; $user = ${'userSignIn'.$site}; echo "$host, $user"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/214033-use-variable-connection-strings-without-eval/#findComment-1113967 Share on other sites More sharing options...
mottwsc Posted September 22, 2010 Author Share Posted September 22, 2010 Thanks - that will work! Quote Link to comment https://forums.phpfreaks.com/topic/214033-use-variable-connection-strings-without-eval/#findComment-1114026 Share on other sites More sharing options...
Adam Posted September 22, 2010 Share Posted September 22, 2010 As PFMaBiSmAd suggested, I think an array would be better for something like this: $cnxs = array( 1 => array( 'host' => 'server1', 'user' => 'xxx1', 'password' => 'yyy1', 'dbname' => 'zzz1' ), 2 => array( 'host' => 'server2', 'user' => 'xxx2', 'password' => 'yyy2', 'dbname' => 'zzz2' ) ); $site = 2; if (!$cxnSignIn = mysqli_connect($cnxs[$site]['host'], $cnxs[$site]['user'], $cnxs[$site]['password'], $cnxs[$site]['dbname'])) Quote Link to comment https://forums.phpfreaks.com/topic/214033-use-variable-connection-strings-without-eval/#findComment-1114030 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.