0

First of all, I'm not sure if the title is correct but here is what I want to do. I want to use array selected from DB and use it inside for loop. When I dump the array (//print_r($ns1);) it is available. However print_r($host); dump have no data when I print_r it. I think the problem is in $host[] = "@$ns1 $subdomain"; but I'm not so sure. Why I can't print_r($host);? Please help and thanks in advance.

$sql = "SELECT subdomain, ns1, ns2, id_user, counter FROM ns WHERE counter = 1";
$result = mysqli_query($con,$sql);

$subdomain = array();
$ns1 = array();
$ns2 = array();
$id_user = array();
$counter = array();

// Associative array
while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){;
$subdomain []= $row['subdomain'];
$ns1 []= $row['ns1'];
$ns2 []= $row['ns2'];
$id_User[]= $row['id_user'];
$counter []= $row['counter'];
}

$total= count($subdomain);

$host = array();
$ip = array();
$mx = array();

for ($x = 1; $x <= $total; $x++) {

//print_r($subdomain); // both data is available
//print_r($ns1);

$host[] = "@$ns1 $subdomain";
print_r($host);
$ip = `/usr/bin/dig $host +short A`;
$mx = `/usr/bin/dig $host +short MX`;
print_r($host);

}

1 Answer 1

2

In your code, $ns1 and $subdomain are arrays, yet you're using them as if they are strings. When you do this, the only thing that PHP will out put is an array to string conversion notice, while returning a value of Array. If you want to access the data in your arrays, you will need to do something akin to:

$host[] = "@" . $ns1[$x] . " " . $subdomain[$x];

Again, when attempting to access data in the host array you will need to supply an index if you want to access the data in it:

$ip = `/usr/bin/dig $host[$x] +short A`;
$mx = `/usr/bin/dig $host[$x] +short MX`;

Though, I suspect $host should be a string and not an array:

for ($x = 1; $x <= $total; $x++) {
    $host = "@" . $ns1[$x] . " " . $subdomain[$x];
    $ip = `/usr/bin/dig $host +short A`;
    $mx = `/usr/bin/dig $host +short MX`;
}

When you have a moment, review the documentation on array access in PHP.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.