如何在关闭之前检查mysqli连接是否打开 [英] How to Check Whether mysqli connection is open before closing it

查看:69
本文介绍了如何在关闭之前检查mysqli连接是否打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用mysqli_close($connection)关闭$connection.但是在关闭之前,我需要确保相关的连接处于打开状态.

I am going to use mysqli_close($connection) to close the $connection. But Before Closing I need to ensure that the concerned connection is open.

我尝试过

if($connection)
{
  mysqli_close($connection);
}

但是它不起作用.有解决方案吗?

But it is not working. Any Solution?

推荐答案

旧问题,但可能会对其他人有所帮助. 这来自 PHP.net .

Old question, but may help someone else. This is from PHP.net.

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

/* check if server is alive */
if ($mysqli->ping()) {
    printf ("Our connection is ok!\n");
} else {
    printf ("Error: %s\n", $mysqli->error);
}

/* close connection */
$mysqli->close();
?>

程序样式

<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* check if server is alive */
if (mysqli_ping($link)) {
    printf ("Our connection is ok!\n");
} else {
    printf ("Error: %s\n", mysqli_error($link));
}

/* close connection */
mysqli_close($link);
?>

这篇关于如何在关闭之前检查mysqli连接是否打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆