I have a unique column in database which is named ip
IP addresses are stored in this column as BINARY(16)
(with no collation) after converting them using the PHP function
$store_ip = inet_pton($ip);
When I try to insert the same IP twice it works fine and fails because it is unique,
But when I try to select the IP it doesn't work and always returns FALSE (not found)
<?php
try {
$ip = inet_pton($_SERVER['REMOTE_ADDR']);
$stmt = $db->prepare("SELECT * FROM `votes` WHERE ip=?");
$stmt->execute([$ip]);
$get = $stmt->fetch();
if( ! $get){
echo 'Not found';
}else{
echo 'Found';
}
// close connection
$get = null;
$stmt = null;
} catch (PDOException $e) {
error_log($e->getMessage());
}
The part where I insert the IP:
<?php
if( ! filter_var($ip, FILTER_VALIDATE_IP)){
return FALSE;
}
$ip = inet_pton($_SERVER['REMOTE_ADDR']);
try {
$stmt = $db->prepare("INSERT INTO votes(ip, answer) VALUES(?,?)");
$stmt->execute([$ip, $answer]);
$stmt = null;
} catch (PDOException $e) {
return FALSE;
}
from Cannot select where ip=inet_pton($ip)
No comments:
Post a Comment