Jump to content

Archived

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

smith.james0

Loading column into signle array help

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
can you search using sql queries?? then you don't even need to load an array

Share this post


Link to post
Share on other sites
We need a little more information before we can suggest the best coding method.

What is your goal with searching the IP addresses?

What is the purpose of the script?

How are the tables in your database structured?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.