我有一个文件myfunctions.php,它有四个函数。所有函数在调用时都会对数据库执行一些事务。如果我只从那个页面调用一个函数,它就可以正常工作了。但是,如果im调用两个函数而不是一个最上面的函数(首先调用的函数)是工作的,而另一个是无效的。
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,将其声明为全局函数,并且它们都具有类似的性质。
文件,其中im调用这些函数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),那么第二个函数是不工作的,如果我替换它们的位置,问题是相同的。事先鸣谢
发布于 2014-09-12 21:06:26
关闭两个函数中的mysql连接,只需忽略$mysqli -> close();。
发布于 2014-09-12 21:07:09
这是因为在每个函数的末尾关闭$mysqli,应该在脚本结束时关闭
https://stackoverflow.com/questions/25816791
复制相似问题