我有此代码,我想开始与该数据库的新连接,但在以前的数据库连接中具有不同的值,但是无论何时我启动该连接,我在网站上收到的错误说“无法获取mysqli”,请帮助我不知道该怎么办
我尝试使用“ mysqli_close($ conn);”关闭与数据库的连接。但没有用
$result = mysqli_query($conn, "SELECT * FROM userissues, response WHERE response.idmatch = userissues.emailcount");
$count = mysqli_num_rows($result);
if($count == 0){
mysqli_close($conn);
$result = mysqli_query($conn,"SELECT * FROM userissues WHERE departmentSent = '$_SESSION[user_email]' ORDER BY dateSent DESC");
$count = mysqli_num_rows($result);
if($count == 0){
echo '<p style="margin-left: 162px; width: 300px; float: left; height: 50px;">you have no emails</p>';
}else{
while($row = mysqli_fetch_array($result))
{
?>
<a href="email_preview.php?id=<?php echo $row['emailcount'];?>&site=inbox">
<?php
echo '
<table border="1" id="'.$row["emailcount"].'" style="margin-left: 162px; width: 300px; float: left; height: 50px;">
<tr><td>'.$row["user_email"].'</td></tr><br>
<tr><td>'.$row["dateSent"].'</td></tr><br>
<tr><td>'.$row["subject"].'</td></tr><br>
<tr><td> '.$row["report"].'</td></tr><br><br>
</table>
</a>
';
}
}
mysqli_close($conn);
}elseif($count > 0){
mysqli_close($conn);
$result = mysqli_query($conn,"SELECT * FROM userissues, response WHERE userissues.departmentSent = '$_SESSION[user_email]' AND response.user_receiver = '$_SESSION[user_email]' ORDER BY userissues.dateSent AND response.dateResponded DESC");
$count = mysqli_num_rows($result);
if($count == 0){
echo '<p style="margin-left: 162px; width: 300px; float: left; height: 50px;">you have no emails</p>';
}else{
while($row = mysqli_fetch_array($result)){
echo'';
}
}
}
我只想建立到相同数据库但值不同的新连接
答案 0 :(得分:1)
如果关闭连接mysqli_close($conn);
,则无法再使用该连接发出查询。基本上,这将破坏您可以用来发出查询和检索结果的管道。
我认为您真正想做的是mysqli_free_result($result)
documentation for
mysqli_free_result()
您的脚本向SQL Injection Attack开放 甚至if you are escaping inputs, its not safe! 在
MYSQLI_
或PDO
API中使用prepared parameterized statements
如果确实需要关闭与数据库mysqli_close($conn);
的连接,则必须发出另一个$con = mysqli_connect()
来创建另一个连接。但是,这是一个相对较慢的过程,一个脚本实际上应该只打开一个连接。