I have sets of variables storing different settings. How to use a specific set depending on the occasion?

1

I have this config file.

<?PHP
$s_ipserver1 = "10.0.0.101";
$db_porta1    = "3306";
$db_user1     = "user";
$db_password1 = "123456";
$db_name1     = "name";

$s_ipserver2 = "xxx.xxx.xxx.xxx";
$db_porta2    = "3306";
$db_user2     = "user";
$db_password2 = "123456";
$db_name2     = "name";

$s_ipserver3 = "xxx.xxx.xxx.xxx";
$db_porta3    = "3306";
$db_user3     = "user";
$db_password3 = "123456";
$db_name3     = "name";
?>

And I want to make it something like this.

<?PHP

$dbL = "1";// Que servidor é este? 1, 2 ou 3.

$s_ipserver1 = "10.0.0.101";
$db_porta1    = "3306";
$db_user1     = "user";
$db_password1 = "123456";
$db_name1     = "name";

$s_ipserver2 = "xxx.xxx.xxx.xxx";
$db_porta2    = "3306";
$db_user2     = "user";
$db_password2 = "123456";
$db_name2     = "name";

$s_ipserver3 = "xxx.xxx.xxx.xxx";
$db_porta3    = "3306";
$db_user3     = "user";
$db_password3 = "123456";
$db_name3     = "name";

$s_ipserver  = "$s_ipserver$dbL";
$db_porta    = "$db_porta$dbL";
$db_user     = "$db_user$dbL";
$db_password = "$db_password$dbL";
$db_name     = "$db_name$dbL";
?>

This is one of the connection files.

<?PHP
include_once("/pasta/config.php");
$db_host1     = "$s_ipserver1:$db_porta1";
$db_link1     = mysql_connect($db_host1, $db_user1, $db_password1) or die (mysql_error ());
$db_connect1  = mysql_select_db($db_name1, $db_link1);
?>

I need something like this.

<?PHP
include_once("/pasta/config.php");
$db_host      = "$s_ipserver:$db_porta";
$db_link      = mysql_connect($db_host, $db_user, $db_password) or die (mysql_error ());
$db_connect   = mysql_select_db($db_name, $db_link);
?>

Where is my error? All original files are working perfectly.

    
asked by anonymous 09.09.2014 / 20:08

3 answers

0

The code is not the cleanest and most beautiful but it solves my problem when the server itself wants to connect to itself, choosing from 3 possible connections with the small variable $dbL

In /follow/config.php

<?PHP

$dbL = "1";// Que servidor é este? 1, 2 ou 3.

$s_ipserver1 = "10.0.0.101";
$db_porta1    = "3306";
$db_user1     = "user";
$db_password1 = "123456";
$db_name1     = "name";

$s_ipserver2 = "xxx.xxx.xxx.xxx";
$db_porta2    = "3306";
$db_user2     = "user";
$db_password2 = "123456";
$db_name2     = "name";

$s_ipserver3 = "xxx.xxx.xxx.xxx";
$db_porta3    = "3306";
$db_user3     = "user";
$db_password3 = "123456";
$db_name3     = "name";
?>

In /follow/subconnect.php

<?php 
include_once("/pasta/config.php");
$ipserver  = '$s_ipserver'. $dbL;
$porta     = '$db_porta'. $dbL;
$user      = '$db_user'. $dbL;
$password  = '$db_password'. $dbL;
$name      = '$db_name'. $dbL;

eval("\$ipserver = \"$ipserver\";");
eval("\$porta = \"$porta\";");
eval("\$user = \"$user\";");
eval("\$password = \"$password\";");
eval("\$name = \"$name\";");
?>

In /pasta/connect.php

<?PHP
include_once("/pasta/subconnect.php");
$host      = "$ipserver:$porta";
$link      = mysql_connect($host, $user, $password) or die (mysql_error ());
$db_connect   = mysql_select_db($name, $link);
?>

And thank you @bfavaretto for the patience and the code you gave me and the others too.

Correction on request of @bfavaretto

Option to /pasta/subconnect.php

<?php 
include_once("/pasta/config.php");
$ipserver  = 's_ipserver'. $dbL;
$porta     = 'db_porta'. $dbL;
$user      = 'db_user'. $dbL;
$password  = 'db_password'. $dbL;
$name      = 'db_name'. $dbL;
// Sem eval()
?>

Option for /pasta/connect.php For without eval ()

<?PHP
include_once("/pasta/subconnect.php");
$host      = $$ipserver . ':'. $$porta;
$link      = mysql_connect($host, $$user, $$password) or die (mysql_error ());
$db_connect   = mysql_select_db($$name, $link);
?>
    
10.09.2014 / 03:26
8

You can use variable variables , but I find this extremely ugly and confusing:

$dbL = "1";
$s_ipserver1 = "bla";
$servidor = 's_ipserver'. $dbL;
echo $$servidor; // "bla";

It would be more elegant to use arrays:

$servidores = array(
    array(
        's_ipserver' => "10.0.0.101",
        'db_porta'    => "3306",
        'db_user'     => "user",
        'db_password' => "123456",
        'db_name'     => "name",
    ),
    array(
        's_ipserver' => "10.0.0.201",
        'db_porta'    => "3306",
        'db_user'     => "user",
        'db_password' => "98765",
        'db_name'     => "foo"
    )
    // etc
);

$indice = 1;
$servidor = $servidores($indice);
echo $servidor['s_ipserver']; // "10.0.0.201"
    
09.09.2014 / 20:34
3

You can create individual files for each connection, as suggested by @bfavaretto, create an array containing the indexes:

#pasta/config/database.php
return array(
    'DB1' => array(
             's_ipserver'  => '10.0.0.101',
             'db_porta'    => '3306',
             'db_user'     => 'user',
             'db_password' => '123456',
             'db_name'     => 'name',
             ),

    'DB2' => array(
             's_ipserver'  => 'xxx.xxx.xxx.xxx',
             'db_porta'    => '3306',
             'db_user'     => 'user',
             'db_password' => '123456',
             'db_name'     => 'name',
             ),

    'DB3' => array(
             's_ipserver'  => 'xxx.xxx.xxx.xxx',
             'db_porta'    => '3306',
             'db_user'     => 'user',
             'db_password' => '123456',
             'db_name'     => 'name',
             )
);

Exemplifying array with index

# conn database-1
$conn = include_once('/pasta/config/database.php');
$conn = $conn['DB1'];

// output conexão DB1
Array
(
    [s_ipserver] => 10.0.0.101
    [db_porta] => 3306
    [db_user] => user
    [db_password] => 123456
    [db_name] => name
)

Connection example

$host       = $conn['s_ipserver'] . ':' . $conn['db_porta'];
$db_link    = mysql_connect( $host , $conn['db_user'] , $conn['db_password'] ) or die( mysql_error() );
$db_connect = mysql_select_db( $db_name, $db_link );
    
10.09.2014 / 03:48