• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • mysqli::close()

    (PHP 5, PHP 7)

    关闭先前打开的数据库连接

    说明

    面向对象风格
    mysqli::close(void): bool
    过程化风格
    mysqli_close(mysqli$link): bool

    关闭先前打开的数据库连接

    参数

    $link

    仅以过程化样式:由mysqli_connect()或mysqli_init()返回的链接标识。

    返回值

    成功时返回TRUE,或者在失败时返回FALSE

    范例

    See mysqli_connect().

    参见

    • mysqli::__construct() Open a new connection to the MySQL server
    • mysqli_init() 初始化 MySQLi 并返回一个资源类型的值,这个值可以作为 mysqli_real_connect()函数的传入参数
    • mysqli_real_connect() 建立一个 MySQL 服务器连接
    Note with Francis' example, using the function name link() will throw an error at runtime as it is already a function within the language. see: http://php.net/manual/en/function.link.php
    Since a lot of manual examples recommend to use a variable to initiate your connection, it is interesting to know that mysqli_close() will unset that variable, causing further connection attempts to fail.
    ex:
    $link = mysqli_connect($host, $user, $pw);
    if ($link) {
      // Database is reachable
      mysqli_close($link);
    }
    if ($link) {
      // Database unreachable because $link = NULL
    }
    Easiest solution for me is to initiate connection through a function.
    ex:
    function link() {
      global $host;
      global $user;
      global $pw;
      global $link;
      $link = mysqli_connect($host, $user, $pw);
    }
    link();
    // Database is reachable
    mysqli_close($link)
    link();
    // Database is reachable
    mysqli_close($link)
    I've had situations where database connections appeared to persist following php execution. So, now, my __destructor function explicitly contains a $cxn->close(). It hurts nothing, and helps avoid memory leaks.