简体   繁体   中英

How to Save IP address By INET_ATON in CakePHP 2.x?

I want to use MySQL INET_ATON function to save IP address by CakePHP 2.x? My code is here and not work correctly!

$this->loadModel('MyModel');
$data = array();
$ip = '8.8.8.8';
$data['ip'] = 'INET_ATON('.$ip.')';
$this->MyModel->save($data);

And i do not use $this->MyModel->query() .

For Example:

$this->MyModel->query("INSERT INTO `my_table` (`ip`) VALUES (INET_ATON('8.8.8.8'))")

Use This Code. Correctly Work.

$this->loadModel('MyModel');
$data = array();
$ip = '8.8.8.8';
$data['MyModel']['ip'] = DboSource::expression('INET_ATON("'.$ip.'")');
$this->MyModel->save($data);

You can use ip2long function for converting ip to int and it saves in your Db like this:

$this->loadModel('MyModel');
$data = array();
$ip = '8.8.8.8';
$data['MyModel']['ip'] = ip2long($ip);
$this->MyModel->save($data);

Thanks..!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM