duanke9540
2019-08-03 11:05
浏览 72
已采纳

如何使用预准备语句选择IP地址

Im trying to select an IP address using prepared statements with php, if the IP already exist its everything is ok, but if doesnt exist it does never enter inside the if($stmt -> num_rows === 0), it always enter on the elselike if the query is wrong. The field is setted to int unsigned.
So whats wrong with this prepared statement? How can make it enter in the if($stmt -> num_rows === 0) when select doesnt match anything?

$ip_long= ip2long($ip);
$stmt = $mysqli -> prepare('SELECT INET_NTOA(ip), registry_date FROM users WHERE ip = ?');
    if (
        $stmt &&
        $stmt -> bind_param('i', $ip_long) &&
        $stmt -> execute() && 
        $stmt -> store_result() &&
        $stmt -> bind_result($ip, $registry_date_checker) && 
        $stmt -> fetch()
    ) {
       if($stmt -> num_rows === 0) {
          //It does never enter here.
       }
       if($stmt -> num_rows === 1) {
          //If exist enter here.
       }
   }
   else{
       //If select not found anything it always enter here.
       echo "Error mysqli: ".$mysqli-> error; //Returns empty error
       echo "Error stmt : ".$stmt -> error; //Returns empty error
       echo "Numrows: ".$stmt -> num_rows; //Returns 0
       exit;
   }

2条回答 默认 最新

相关推荐 更多相似问题