PHP - Retrieving IPs from host names
up vote
0
down vote
favorite
I am retrieving values from a table field and appending a string to it to get a list of host names. I would then like to loop through all the host names and return the IP for each.
The following code gets my host names:
$sql = "select * from table order by col1 asc";
$q = mysql_query($sql) or die(mysql_error() ." " . $sql);
// Loop through table and append .zzz to each col1.
while ($r = mysql_fetch_array($q)) {
$host_name = $r['col1']. ".zzz" . " ";
//echo $host_name;
}
The above code works as it should, but I am unsure how to retrieve the IPs for each host name. I have been trying gethostbyname but can't get it to work correctly. I know I have to loop through the host name values. Would I use a foreach loop? I have tried that and can't get it working. Can someone give me some pointers please.
I have tried the following but it doesn't return anything:
$host_name_ip = $host_name;
foreach ($host_name_ip as $name) {
$ip = gethostbyname($name);
echo '<li>' . $ip. '</li>';
}
I have also tried this... again, nothing:
foreach($host_name as $url) {
$ip = gethostbyname(trim($url));
}
echo $ip;
Thanks. :)
php ip host gethostbyname
add a comment |
up vote
0
down vote
favorite
I am retrieving values from a table field and appending a string to it to get a list of host names. I would then like to loop through all the host names and return the IP for each.
The following code gets my host names:
$sql = "select * from table order by col1 asc";
$q = mysql_query($sql) or die(mysql_error() ." " . $sql);
// Loop through table and append .zzz to each col1.
while ($r = mysql_fetch_array($q)) {
$host_name = $r['col1']. ".zzz" . " ";
//echo $host_name;
}
The above code works as it should, but I am unsure how to retrieve the IPs for each host name. I have been trying gethostbyname but can't get it to work correctly. I know I have to loop through the host name values. Would I use a foreach loop? I have tried that and can't get it working. Can someone give me some pointers please.
I have tried the following but it doesn't return anything:
$host_name_ip = $host_name;
foreach ($host_name_ip as $name) {
$ip = gethostbyname($name);
echo '<li>' . $ip. '</li>';
}
I have also tried this... again, nothing:
foreach($host_name as $url) {
$ip = gethostbyname(trim($url));
}
echo $ip;
Thanks. :)
php ip host gethostbyname
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am retrieving values from a table field and appending a string to it to get a list of host names. I would then like to loop through all the host names and return the IP for each.
The following code gets my host names:
$sql = "select * from table order by col1 asc";
$q = mysql_query($sql) or die(mysql_error() ." " . $sql);
// Loop through table and append .zzz to each col1.
while ($r = mysql_fetch_array($q)) {
$host_name = $r['col1']. ".zzz" . " ";
//echo $host_name;
}
The above code works as it should, but I am unsure how to retrieve the IPs for each host name. I have been trying gethostbyname but can't get it to work correctly. I know I have to loop through the host name values. Would I use a foreach loop? I have tried that and can't get it working. Can someone give me some pointers please.
I have tried the following but it doesn't return anything:
$host_name_ip = $host_name;
foreach ($host_name_ip as $name) {
$ip = gethostbyname($name);
echo '<li>' . $ip. '</li>';
}
I have also tried this... again, nothing:
foreach($host_name as $url) {
$ip = gethostbyname(trim($url));
}
echo $ip;
Thanks. :)
php ip host gethostbyname
I am retrieving values from a table field and appending a string to it to get a list of host names. I would then like to loop through all the host names and return the IP for each.
The following code gets my host names:
$sql = "select * from table order by col1 asc";
$q = mysql_query($sql) or die(mysql_error() ." " . $sql);
// Loop through table and append .zzz to each col1.
while ($r = mysql_fetch_array($q)) {
$host_name = $r['col1']. ".zzz" . " ";
//echo $host_name;
}
The above code works as it should, but I am unsure how to retrieve the IPs for each host name. I have been trying gethostbyname but can't get it to work correctly. I know I have to loop through the host name values. Would I use a foreach loop? I have tried that and can't get it working. Can someone give me some pointers please.
I have tried the following but it doesn't return anything:
$host_name_ip = $host_name;
foreach ($host_name_ip as $name) {
$ip = gethostbyname($name);
echo '<li>' . $ip. '</li>';
}
I have also tried this... again, nothing:
foreach($host_name as $url) {
$ip = gethostbyname(trim($url));
}
echo $ip;
Thanks. :)
php ip host gethostbyname
php ip host gethostbyname
asked Nov 19 at 20:53
tlmm
13
13
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
You are overwriting the value of $host_name
in your initial loop. Change it to be an array and your foreach will work.
while ($r = mysql_fetch_array($q)) {
$host_name = $r['col1']. ".zzz";
}
To print the host name and it's address in your loop use:
foreach ($host_name_ip as $name) {
$ip = gethostbyname($name);
echo '<li>' . $name . ' is at address ' . $ip. '</li>';
}
You don't even need to store the names in an array. Just do everything you need in your while loop.
while ($r = mysql_fetch_array($q)) {
$ip = gethostbyname($r['col1']. ".zzz");
echo '<li>' . $r['col1']. '.zzz' . ' is at address ' . $ip. '</li>';
}
Thank you. It now returns values to the page but returns the host names and not the IPs.
– tlmm
Nov 19 at 21:00
What is in thecol1
column in your database? If it is a host name remove everything after the]
on the left side of the = in my answer (leaving the;
of course).
– Dave
Nov 19 at 21:02
There are 5 letter variables in col1 for each row. Appending 'zzz' gives me my host name. (zzz is just a sample)
– tlmm
Nov 19 at 21:04
1
OK then remove the. " "
after the.zzz
. you don't need/want it any more.
– Dave
Nov 19 at 21:05
That was the problem! Thanks so much. It works now!!!!
– tlmm
Nov 19 at 21:07
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382474%2fphp-retrieving-ips-from-host-names%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
You are overwriting the value of $host_name
in your initial loop. Change it to be an array and your foreach will work.
while ($r = mysql_fetch_array($q)) {
$host_name = $r['col1']. ".zzz";
}
To print the host name and it's address in your loop use:
foreach ($host_name_ip as $name) {
$ip = gethostbyname($name);
echo '<li>' . $name . ' is at address ' . $ip. '</li>';
}
You don't even need to store the names in an array. Just do everything you need in your while loop.
while ($r = mysql_fetch_array($q)) {
$ip = gethostbyname($r['col1']. ".zzz");
echo '<li>' . $r['col1']. '.zzz' . ' is at address ' . $ip. '</li>';
}
Thank you. It now returns values to the page but returns the host names and not the IPs.
– tlmm
Nov 19 at 21:00
What is in thecol1
column in your database? If it is a host name remove everything after the]
on the left side of the = in my answer (leaving the;
of course).
– Dave
Nov 19 at 21:02
There are 5 letter variables in col1 for each row. Appending 'zzz' gives me my host name. (zzz is just a sample)
– tlmm
Nov 19 at 21:04
1
OK then remove the. " "
after the.zzz
. you don't need/want it any more.
– Dave
Nov 19 at 21:05
That was the problem! Thanks so much. It works now!!!!
– tlmm
Nov 19 at 21:07
|
show 4 more comments
up vote
3
down vote
accepted
You are overwriting the value of $host_name
in your initial loop. Change it to be an array and your foreach will work.
while ($r = mysql_fetch_array($q)) {
$host_name = $r['col1']. ".zzz";
}
To print the host name and it's address in your loop use:
foreach ($host_name_ip as $name) {
$ip = gethostbyname($name);
echo '<li>' . $name . ' is at address ' . $ip. '</li>';
}
You don't even need to store the names in an array. Just do everything you need in your while loop.
while ($r = mysql_fetch_array($q)) {
$ip = gethostbyname($r['col1']. ".zzz");
echo '<li>' . $r['col1']. '.zzz' . ' is at address ' . $ip. '</li>';
}
Thank you. It now returns values to the page but returns the host names and not the IPs.
– tlmm
Nov 19 at 21:00
What is in thecol1
column in your database? If it is a host name remove everything after the]
on the left side of the = in my answer (leaving the;
of course).
– Dave
Nov 19 at 21:02
There are 5 letter variables in col1 for each row. Appending 'zzz' gives me my host name. (zzz is just a sample)
– tlmm
Nov 19 at 21:04
1
OK then remove the. " "
after the.zzz
. you don't need/want it any more.
– Dave
Nov 19 at 21:05
That was the problem! Thanks so much. It works now!!!!
– tlmm
Nov 19 at 21:07
|
show 4 more comments
up vote
3
down vote
accepted
up vote
3
down vote
accepted
You are overwriting the value of $host_name
in your initial loop. Change it to be an array and your foreach will work.
while ($r = mysql_fetch_array($q)) {
$host_name = $r['col1']. ".zzz";
}
To print the host name and it's address in your loop use:
foreach ($host_name_ip as $name) {
$ip = gethostbyname($name);
echo '<li>' . $name . ' is at address ' . $ip. '</li>';
}
You don't even need to store the names in an array. Just do everything you need in your while loop.
while ($r = mysql_fetch_array($q)) {
$ip = gethostbyname($r['col1']. ".zzz");
echo '<li>' . $r['col1']. '.zzz' . ' is at address ' . $ip. '</li>';
}
You are overwriting the value of $host_name
in your initial loop. Change it to be an array and your foreach will work.
while ($r = mysql_fetch_array($q)) {
$host_name = $r['col1']. ".zzz";
}
To print the host name and it's address in your loop use:
foreach ($host_name_ip as $name) {
$ip = gethostbyname($name);
echo '<li>' . $name . ' is at address ' . $ip. '</li>';
}
You don't even need to store the names in an array. Just do everything you need in your while loop.
while ($r = mysql_fetch_array($q)) {
$ip = gethostbyname($r['col1']. ".zzz");
echo '<li>' . $r['col1']. '.zzz' . ' is at address ' . $ip. '</li>';
}
edited Nov 20 at 14:06
answered Nov 19 at 20:57
Dave
2,14551525
2,14551525
Thank you. It now returns values to the page but returns the host names and not the IPs.
– tlmm
Nov 19 at 21:00
What is in thecol1
column in your database? If it is a host name remove everything after the]
on the left side of the = in my answer (leaving the;
of course).
– Dave
Nov 19 at 21:02
There are 5 letter variables in col1 for each row. Appending 'zzz' gives me my host name. (zzz is just a sample)
– tlmm
Nov 19 at 21:04
1
OK then remove the. " "
after the.zzz
. you don't need/want it any more.
– Dave
Nov 19 at 21:05
That was the problem! Thanks so much. It works now!!!!
– tlmm
Nov 19 at 21:07
|
show 4 more comments
Thank you. It now returns values to the page but returns the host names and not the IPs.
– tlmm
Nov 19 at 21:00
What is in thecol1
column in your database? If it is a host name remove everything after the]
on the left side of the = in my answer (leaving the;
of course).
– Dave
Nov 19 at 21:02
There are 5 letter variables in col1 for each row. Appending 'zzz' gives me my host name. (zzz is just a sample)
– tlmm
Nov 19 at 21:04
1
OK then remove the. " "
after the.zzz
. you don't need/want it any more.
– Dave
Nov 19 at 21:05
That was the problem! Thanks so much. It works now!!!!
– tlmm
Nov 19 at 21:07
Thank you. It now returns values to the page but returns the host names and not the IPs.
– tlmm
Nov 19 at 21:00
Thank you. It now returns values to the page but returns the host names and not the IPs.
– tlmm
Nov 19 at 21:00
What is in the
col1
column in your database? If it is a host name remove everything after the ]
on the left side of the = in my answer (leaving the ;
of course).– Dave
Nov 19 at 21:02
What is in the
col1
column in your database? If it is a host name remove everything after the ]
on the left side of the = in my answer (leaving the ;
of course).– Dave
Nov 19 at 21:02
There are 5 letter variables in col1 for each row. Appending 'zzz' gives me my host name. (zzz is just a sample)
– tlmm
Nov 19 at 21:04
There are 5 letter variables in col1 for each row. Appending 'zzz' gives me my host name. (zzz is just a sample)
– tlmm
Nov 19 at 21:04
1
1
OK then remove the
. " "
after the .zzz
. you don't need/want it any more.– Dave
Nov 19 at 21:05
OK then remove the
. " "
after the .zzz
. you don't need/want it any more.– Dave
Nov 19 at 21:05
That was the problem! Thanks so much. It works now!!!!
– tlmm
Nov 19 at 21:07
That was the problem! Thanks so much. It works now!!!!
– tlmm
Nov 19 at 21:07
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382474%2fphp-retrieving-ips-from-host-names%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown