我有一个文件 myfunctions.php,它有四个函数。所有函数在调用时都会对数据库执行一些事务。如果我只从该页面调用一个工作正常的函数。但如果我调用两个函数,那么只有一个顶级函数(首先调用的函数)正在工作,而其他函数则不起作用。
myfunctions.php
<?php
include('includes/db.inc.php');
function blockip($ip,$attempts){
global $mysqli;
if(isset($ip) && $ip!=''){
$query = 'INSERT INTO blockip VALUES(?,?)';
if($stmt = $mysqli -> prepare($query)){
$stmt -> bind_param('si',$ip,$attempts);
$stmt -> execute();
$stmt -> close();
}
$mysqli -> close();
}else{
echo $mysqli-> error();
}
}
function getip($ip){
global $mysqli;
$ip_found = null;
if(isset($ip) && $ip!=''){
$query = 'SELECT `ip` FROM `blockip` WHERE `ip`=?';
if($stmt=$mysqli -> prepare($query)){
$stmt -> bind_param('s',$ip);
$stmt -> execute();
$stmt -> bind_result($ipret);
$stmt -> fetch();
$stmt -> close();
if($ipret!=null)
$ip_found = $ipret;
else
$ip_fount = '';
}
$mysqli -> close();
}
return $ip_found;
}
?>
此文件中的其他函数也通过将其声明为全局来使用 $mysqli,并且所有函数都具有相似的性质。
我调用这些函数的文件login.php
<?php
session_start();
include('myfunctions.php');
$client_ip = $_SERVER['REMOTE_ADDR'];
?>
<!DOCTYPE html>
<html>
<!-- Some HTML Code HERE -->
</html>
<?php
if($_SESSION['errorcode']==1){
echo '<center> Invalid Captcha </center>';
}elseif($_SESSION['errorcode']==2){
echo '<center> Invalid UserName / Password Combination </center>';
trygetip($client_ip);
}
if($_SESSION['errorcode']==3){
session_destroy();
header('Location: 403.html');
exit();
}
?>
我的数据库文件db.inc.php
<?php
error_reporting(0);
$db="xxxxxx";
$host="xxxxxx";
$username="xxxx";
$password="xxxx";
$mysqli = new mysqli($host,$username,$password,$db);
if($mysqli->connect_error){
die('Connection Failed : '.$mysqli-> connect_errno.' : '.$mysqli -> connect_error);
}
?>
现在,如果我使用两个函数 getip($someiphere) 和 bockip($someiphere,$attempts) ,那么第二个函数就不起作用,如果我更换它们的位置,也会出现同样的问题。 提前致谢
最佳答案
在这两个函数中都关闭mysql连接,只需省略$mysqli -> close();
即可。
关于php - 调用 PHP 的其他函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25816791/