retrieve a client name in a multidimensional array

2

I am creating a script to execute procedures in the database of some clients, I have the following arrays with the credentials of each one:

$hosts["Cliente1"] =  array("ip"=> "ip_do_cliente", "usuario" => "usuario","senha" => "12345","base" => "conadmin","porta" => "3306");
$hosts["Cliente2"] =  array("ip"=> "ip_do_cliente", "usuario" => "usuario","senha" => "12345","base" => "conadmin","porta" => "3306");
$hosts["Cliente3"] = array("ip"=> "ip_do_cliente", "usuario" => "usuario","senha" => "12345","base" => "conadmin","porta" => "3306");

I make a foreach to connect in each of them, this way:

foreach ($hosts as $key=>$host) 
    {
        $conn = mysqli_connect($host['ip'], $host['usuario'], $host['senha'], $host['base'], $host['porta']);
    }

If successful, I'll do a query to save to my database, like this:

            $data = date('d/m/Y H:i:s');
            $ip_cliente = $host['ip'];
            $cliente = ????; 
            $mensagem = 'Teste Executado com sucesso!';

            $query = sprintf("INSERT INTO pbx_teste_permissao(ip_cliente, data_teste, mensagem)"
                    . "values('%s','%s','%s', '%s')",$ip_cliente, $data, $cliente, $mensagem);

            pg_query($query);

I also wanted to store the client name, how could I return the foreach from the client name and populate the $ client variable?

    
asked by anonymous 12.01.2018 / 20:29

1 answer

0

If the client name is the user item in your array use it, otherwise the name would be:

s["Cliente1"] =  array("ip"=> "ip_do_cliente", "usuario" => "usuario", "nome_do_cliente" => "nome_do_cliente", senha" => "12345","base" => "conadmin","porta" => "3306");

then inside the foreach define the client variable:

foreach ($hosts as $key=>$host) 
    {    
        $nome_do_cliente= $host['nome_do_cliente'];
        $conn = mysqli_connect($host['ip'], $host['usuario'], $host['senha'], $host['base'], $host['porta']);
    }

When it does insert:

$data = date('d/m/Y H:i:s');
            $ip_cliente = $host['ip'];
            $cliente = $nome_do_cliente; 
            $mensagem = 'Teste Executado com sucesso!';

            $query = sprintf("INSERT INTO pbx_teste_permissao(ip_cliente, data_teste, mensagem)"
                    . "values('%s','%s','%s', '%s')",$ip_cliente, $data, $cliente, $mensagem);

            pg_query($query);
    
12.01.2018 / 20:53