inserting a new value to mysql via php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.whatismyip.com/automation/n09230945.asp");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$current_new_ip = curl_exec($ch);
curl_close ($ch);
mysql_query("INSERT INTO ip_table VALUES ('1','".$current_new_ip."', '1')");

mysql:

CREATE TABLE ip_table (                                 
            id int(11) NOT NULL AUTO_INCREMENT,                   
            ip_number int(11) DEFAULT NULL,
            used int(11) DEFAULT NULL,                                
            PRIMARY KEY (id)
          )

actually, ip is : 56.78.123.90

however these php codes are inserting: 5678

i wanna insert ip as 56.78.123.90, how can i do it?

Upvotes: 0

Views: 112

Answers (3)

Crozin
Crozin

Reputation: 44406

  1. Keep in mind that IPv4 (32-bit) will be replaced by IPv6 (128-bit) so using INT type (32-bit long) might cause some problems in near feature.
  2. Use INET_ATON() and INET_NTOA() functions to convert from number to text

Upvotes: 0

Xavinou
Xavinou

Reputation: 802

ip_number must be a varchar(15), not a integer ;)

Upvotes: 1

hsz
hsz

Reputation: 152304

Change ip_number column's type to varchar(15) - because your IP address is a string - not an integer.

CREATE TABLE ip_table (                                 
        id int(11) NOT NULL AUTO_INCREMENT,                   
        ip_number varchar(15) DEFAULT NULL,
        used int(11) DEFAULT NULL,                                
        PRIMARY KEY (id)
      )

Upvotes: 2

Related Questions