我使用include_once调用一个名为getTrysLeft的外部文件中的函数,该文件检查用户在最近的2小时内尝试登录的次数。问题是它总是返回0.如果我通过PHPMyAdmin运行SQL它返回行但我的代码总是返回0.导致它显示错误行数的错误是什么?以下是相关代码:
<?php
include_once 'db_connect.php';
include_once 'functions.php';
...
elseif ($loginstatus == 2)
{
$now = time();
$valid_attempts = $now - (2 * 60 * 60);
$numberofrows = getTrysLeft($user_id, $valid_attempts, $mysqli); /// Calling function located in functions.php
$error = '<p class="error">The email address and the password does not match, try again. There are currently: '.$numberofrows.' attemts.</p>'; /// Error value, here I always see "0 attempts"
$_SESSION['lgnerror']= $error;
header('Location: ../login.php');
exit();
}
以下是getTrysLeft函数:
function getTrysLeft($user_id, $valid_attempts, $mysqli)
{
if ($stmt = $mysqli->prepare("SELECT time FROM login_attempts WHERE user_id = ? AND time > ?"))
{
$stmt->bind_param('ii', $user_id, $valid_attempts);
$stmt->execute();
$stmt->store_result();
$numrows = $stmt->num_rows;
$stmt->close();
return $numrows;
}
else
{
/// Some error handling here, nothing relevant really
}
}
那么你说错误是什么?为什么num_rows返回0,即使完全相同的SQL语句返回PHPMyAdmin中的行?