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

Asked

Viewed 181 times

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 turn it into 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 link 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);
?>

And I need it to look 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 mistake? All the original files are working perfectly.

3 answers

8

You can use variable variables, but I find it 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"
  • 1

    You must be a bot, only can :D

  • 3

    bots don’t find anything extremely ugly and confusing :)

  • As I can not only write "Good", then "Good!" p

  • I tried the ugly part and the result was the following error in the PHP Warning link file: mysql_connect(): Unknown Mysql server host '$s_ipserver1' (25) in /folder/connect.php on line 4

  • @Rjpserver How was your line $db_link = mysql_connect...?

  • Same as $link = mysql_connect($host, $user, $password) or die (mysql_error ());

  • Didn’t you adapt my code? Just like I showed you $servidor = 's_ipserver'. $dbL;, you need to do it the same way for port, user, etc. Then you can do it $host = $$servidor . ':' . $$porta;, and only use there $host when connecting. @Rjpserver

  • Yes I adapted and created another file to ensure your changes. I can answer this question with all the code I need so we can make the necessary changes or I have to edit the question ??

  • @Rjpserver Answer no, and editing the question would change the nature of it. Give me a link with your code in Pastebin.com

  • I never used Pastebin but it is here in TXT format on my server link

  • $host = $$ipserver . ':'. $$porta; @Rjpservidor

  • Yes I had already tried and the mistake is: PHP Notice: Undefined variable: $s_ipserver1 in /pasta/connect.php on line 3 PHP Notice: Undefined variable: $db_porta1 in /pasta/connect.php on line 3 PHP Warning: mysql_connect(): Access denied for user '$db_user1'@'localhost' (using password: YES) in /pasta/connect.php on line 4 Access denied for user '$db_user1'@'localhost' (using password: YES)

  • I’ve already made the change in the link with what I’ve been doing

  • I don’t know what it could be, @Rjpservidor... :(

  • I’ve done it now as I answer my own question?

  • Yes, you can reply @Rjp

  • 1

    @Rjpserver Seeing your answer touched me on what was wrong with the code you linked: in subconnect.php, use 's_ipserver'. $dbL, dollar sign at the beginning, and take the dollar sign off the names of the other variables as well. In my opinion it’s still a little cleaner than using the eval...

Show 12 more comments

3

You can create individual files for each connection, u as @bfavaretto suggested, 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',
             )
);

Example of the 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 );

0


The code is not cleaner and beautiful but it solves my problem that when the server itself wants to connect to itself by choosing from 3 possible links with the small variable $dbL

In /folder/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 /folder/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 /folder/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 thanks @bfavaretto for the patience and the code you gave me and the others too.

Correction at @bfavaretto’s request

Option for /folder/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 /folder/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);
?>
  • Try to adapt your code to the use of arrays, as @bfavaretto showed in his reply. Avoid using eval at all costs, it is a bad practice and should only be used as a last resort (which is not your case).

  • 3

    "If Eval() is the answer, you are surely asking the wrong question." - http://php.net/manual/en/function.eval.php#44008

  • 3

    @gmsantos, how is that? "-I have a problem. -I know, I will use Regex. >Now you have two problems."

  • @brasofilo yeah, lacked a regex ai to validate the IP config... will ...

  • @gmsantos the problem is that I already have 80 or 90 files dependent on config.php and by switching to an array that would be much better served all would stop working and had to start my entire war from the beginning and I already have a few months of research and build on this because I’m not a programmer or anything like.

  • "I am not a programmer or anything like that", accept the advice to adapt the)

  • "I already have 80 or 90 files dependent on config.php"Hint then, leave the 2 things in config.php, the array and the variables (you can even mount the array by picking the variables, so you don’t have to update in two places), so you’ll be slowly editing the new format, without the old one stopping working. Thus, you eliminate variables only when everything is working with array, and take no risks.

  • I appreciate your strength and with this type of connection I have already started to delete files because there were many who do the same thing only from different servers when I finish I will expose file to file to convert to array because config and connect are not quite as I am show

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.