recover a client name in a multidimensional array

Asked

Viewed 63 times

2

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

$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']);
    }

In case of success, I do a query to save in my database,:

            $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 wanted to store also the client name, as I could return from the foreach the client name and fill in the variable $client ?

1 answer

0


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

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

after inside the foreach set 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 He Goes To Do The 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);
  • 1

    there’s only one thing I need to go along with you, that Insert comes inside the foreach or outside?

  • he goes inside the foreach, sorry when I went to put the code here I did not heed the key detail.

  • of good my answer works for you then, if it had not been necessary an array,

  • got it ! Thank you very much Julio !

Browser other questions tagged

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