为什么这个查询不能通过php运行?

时间:2013-01-14 07:58:56

标签: php mysql

我有:

$con = mysql_connect($db_server,$db_username,$db_password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db($db_db, $con);
  mysql_query("INSERT INTO failed_logins SET username = 'pachonk', ip_address = INET_ATON('$ip'), attempted = CURRENT_TIMESTAMP");
 // mysql_close($con);
echo $ip;

$count = get_attempts(); // Get the Number of Attempts
mysql_close($con);
sleep((2 ^ intval($count)) - 1);

function get_attempts(){
    $result = mysql_query("SELECT * FROM failed_logins WHERE ip_address = INET_ATON('$ip')");
    if(mysql_num_rows($result) > 0)
    {
        $num_rows = mysql_num_rows($result);
        return $num_rows;
    }
    else
    {
    echo NO;
        return 0;
    }
}

当我在我的SQL服务器上运行第二个查询时,它会按我想要的方式运行,但是当我通过php查询时,我得到0行。如果我查询" username = pachonk",它也是我想要的完美方式。发生了什么事?

2 个答案:

答案 0 :(得分:0)

如果$ ip不为空(在您的示例中,$ ip为空),您可以尝试在get_attempts($ ip)中修改函数get_attempts();

答案 1 :(得分:0)

你的插入查询应该或多或少看起来像这样:

mysql_query("INSERT INTO failed_logins values('pachonk', INET_ATON('$ip'), CURRENT_TIMESTAMP");

然后你的选择会给你一些结果