mysql_close()究竟做了什么?

时间:2014-10-25 08:15:13

标签: php

说明:

假设我从php页面获取一些数据,如下所示

<?php  

include 'connect_to_database.php';
while ($data = mysqli_fetch_array(mysqli_query($con,"select * from myTable"),MYSQLI_BOTH))
{
   echo $data['product'];
}

mysqli_close($con);

?>

现在mysql_close($con);在那里做什么只是关闭了这个页面的连接,或者整个网站与数据库断开连接,数据库必须再次连接才能工作..更多我读写{{1在PHP的末尾是一个非常好的做法,好像我们不这样做,然后页面继续在服务器中执行因此占用空间......

有人可以解释一下吗?

2 个答案:

答案 0 :(得分:1)

它关闭变量$con引用的连接。可以有多个连接,然后只关闭你在mysqli_close()的第一个参数中指定的连接。

答案 1 :(得分:1)

mysqli_close()仅关闭与该特定页面的数据库的连接,没有任何内容,例如disconnecting the website from database which has to be connected again to work,每个页面上都会启动一个新连接,并且一旦执行结束,连接就会自动关闭。 mysqli_close()将在执行结束前关闭它,以便演示此

<?php
// connection starts here
mysqli_query($link, "whatever stuff you need to do");
?>
// The connection is closed here 

另一个如何使用mysqli_close的例子

<?php
// connection starts here
mysqli_query($link, "whatever stuff you need to do");
mysqli_close($link); // the connection is closed here 
// do whatever more stuff you have to do that are unrelated to the database
?>

至于退出,它的唯一用途是打破代码,将它放在代码的末尾是没有用的 以下这种方式使用它将毫无用处:

<?php
echo 'Hi';
echo 'Bye';
exit(); // this is useless because all code has been executed already 
?>

以下示例很有用:

<?php
echo 'Hi';
exit(); // this is useful as it stops the execution of whatever code is below it. 
echo 'Bye';
?>