简体   繁体   中英

Where clause in MySQL using VARBINARY column

I have a column in MySQL ip_address which is VARBINARY(16) , and I'm trying to do a where clause conditional on it in PHP :

       $ip_address = inet_pton($ip_address);

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

       ...

However, this is returning 0 rows. What am I doing wrong?

Try using a 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();

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