PHP MySQL : INET_ATON does not save data?

标签 php mysql ip php-5.4 inet-aton

我正在尝试将 IP 地址存储到我的数据库中,并且我正在使用 INET_ATON,如下所示,

$sql = "
INSERT INTO pin (
    page_id,
    user_id,
    pc_name,
    ip,
    geolocation,
    created_on
)VALUES(    
    ?,
    ?,
    ?,
    ?,
    ?,  
    NOW()
)";


$result = $this->connection->executeSQL($sql,array(
$this->page_id,
$this->user_id,
$this->getComputerName(),
'INET_ATON("123.136.106.104")',
$this->getGeoLocation()
));

但我无法将其转换或保存到数据库中的 ip 字段中。我得到 0 而不是数字。

我该怎么办?我对 SQL 做错了什么?

最佳答案

您的问题不在于 INET_ATON 函数 - 而是在于 PDO (我假设它是 PDO)如何解释绑定(bind)参数。您可以这样做:

$sql = "
INSERT INTO pin (
    page_id,
    user_id,
    pc_name,
    ip,
    geolocation,
    created_on
)VALUES(    
    ?,
    ?,
    INET_ATON(?),
    ?,
    ?,  
    NOW()
)";

并传递您的 IP:

$result = $this->connection->executeSQL($sql,array(
$this->page_id,
$this->user_id,
$this->getComputerName(),
"123.136.106.104",
$this->getGeoLocation()
));

关于PHP MySQL : INET_ATON does not save data?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19087546/

相关文章:

geolocation - 地理定位 IP 范围多年来是否发生了变化?

php - 事件记录的目的是什么?

sockets - 在 Windows 中如何检测哪个程序正在监听 TCP/IP 端口?

php - 使用 git 和 php 进行开发(网络开发)

mysql - 比较并获取唯一数据,以逗号分隔存储

MySQL 将 IN 替换为 EXISTS,编辑后的查询开销更大

Python - 将输出作为字典返回 - MySQL

php - 在 php 中将 ip 转换为整数的正确方法是什么?

php - 在 html 表中传递 $_POST ["variable"]

PHP - echo $row 产生相同的输出