Jump to content

Loading column into signle array help


smith.james0

Recommended Posts

I have searched the forum and the web but I am not getting to far.

I need to load all the ip addesses (one column) out of my db into a single array so I can search it.

I have found how to load it multiple arrays but this is no good.

Can anyone point me in the right direction?

Many thanks James
Link to comment
https://forums.phpfreaks.com/topic/15193-loading-column-into-signle-array-help/
Share on other sites

To load them in a single array try...

[code]<?php

//Find total number of ips
$query = "SELECT ip FROM table";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$count = mysql_numrows($result);

$ips = array();

$x = 0;
while ($x < $count):
$ipaddress = mysql_result($result, $x, 'ipaddress');
$ips[$x] = $ipaddress;
$x++;
endwhile;

?>[/code]

Is that what you were looking for?

-Chris
mysql_fetch row() is more efficient than mysq_result().

[code]<?php
$query = "SELECT ip FROM table";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$ips = array();
while ($row = mysql_fetch_row($result)) {
$ips[] = $row[0];
}
?>[/code]
I am loading all the IP addresses into the array then searching the array for IP addresses which are currently online. Then change the font colour of those online. Part of the admin panel i am making. Part of the reason is to learn more about arrays as i haven't used them yet.


James
Well loading them into an array is as simple as
[code]
<?php
$find = "SELECT ip FROM table";
$result = mysql_query($find);

$ips = array();
while($row = mysql_fetch_assoc())
{
  $ips[] = $row['ip'];
}

//View the ips
print_r($ips);
?>
[/code]

sorry Barand I didn't notice you wrote almost the exact same code

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.