PHP Help

Associate
Joined
4 May 2003
Posts
582
Guys.

I have a database table that contains a field, lets call it x. The field x contains a n ip address number.

I wish to retrieve this and analyse only the first octet.

For instance in the IP 192.168.0.1 I only want to analyse the 192 part. I have been using the PHP command split to do this. This I believe splits each IP address into an array containing four values (one for each octet).

I have the following sql select statement.

select x from table_name;

I then use mysql_fetch_array to retrieve the IP address values.

This is where I get stuck.

I have two more variables $y and $z which are integers, I wish to evaluate each value I retrieved from the select statement to see if it is greater that 65. If it is greater than 65 I wish $y to increase by one and if it is less that 65 I wish $z to increase by one.

Does Anyone have any pointers? I am pulling my hair out here :D

Thank You In Advance
 
Nim said:
If you have a variable $ip, then the following line will increment $y if the first octet is >65, and increment $z otherwise.

Code:
intval(substr($ip, 0, strpos($ip, '.'))) > 65 ? $y++ : $z++;

Since you're getting the row out of a database, chances are that the variable is in something like $row['ip'], instead, so you'll need to allocate that to $ip first.

If you just post your code, we can sort it out in about 2 minutes :)

My Code Is As Follows:

Code:
Code To Connect To Database;

$sql = select x from table;

$result = mysql_query($sql);

$ip_address = mysql_fetch_array($result);

Thats As Far As I can get, I now need to enter the code to split the IP address so I only look at the first octet and then the code to increment $y or $z.
 
Back
Top Bottom