MySQL中的Where子句使用VARBINARY列

时间:2014-03-06 01:09:20

标签: php mysql binary

我在MySQL ip_address中有一个列VARBINARY(16),我正试图在PHP中对where子句进行条件化处理:

       $ip_address = inet_pton($ip_address);

       $SQL = "SELECT location,
                      added
                 FROM ips
                WHERE ip_address = BINARY '" . $ip_address . "'";

       ...

但是,这会返回0行。我做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试使用prepared statement

$stmt = $conn->stmt_init();
$SQL = "SELECT location, added FROM ips WHERE ip_address = BINARY ?";
$stmt->prepare($sql);
$stmt->bind_param('s', $ip_address);
$stmt->bind_result($location, $added);
$stmt->execute();
$stmt->fetch();