Jump to content


Photo

Loading column into signle array help


  • Please log in to reply
6 replies to this topic

#1 smith.james0

smith.james0
  • Members
  • PipPipPip
  • Advanced Member
  • 122 posts

Posted 20 July 2006 - 08:24 PM

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

#2 cmgmyr

cmgmyr
  • Members
  • PipPipPip
  • Advanced Member
  • 1,278 posts
  • LocationUSA

Posted 20 July 2006 - 08:30 PM

To load them in a single array try...

<?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;

?>

Is that what you were looking for?

-Chris

#3 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 20 July 2006 - 08:31 PM

can you search using sql queries?? then you don't even need to load an array
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#4 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 20 July 2006 - 08:33 PM

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?

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 20 July 2006 - 08:40 PM

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

<?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];
	}
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 smith.james0

smith.james0
  • Members
  • PipPipPip
  • Advanced Member
  • 122 posts

Posted 20 July 2006 - 08:53 PM

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

#7 boralyl

boralyl
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 21 July 2006 - 01:28 AM

Well loading them into an array is as simple as
<?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);
?>

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users