php 如何在关闭之前检查mysqli连接是否打开
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16319306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Check Whether mysqli connection is open before closing it
提问by Munib
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.
我将mysqli_close($connection)
用来关闭$connection
. 但在关闭之前,我需要确保相关连接是打开的。
I tried
我试过
if($connection)
{
mysqli_close($connection);
}
But it is not working. Any Solution?
但它不起作用。任何解决方案?
回答by user2060451
Old question, but may help someone else. This is from PHP.net.
老问题,但可能会帮助别人。这是来自PHP.net。
Object oriented style
面向对象的风格
<?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();
?>
Procedural style
程序风格
<?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);
?>
回答by Leopoldo Sanczyk
While some suggest to check for $connection->ping()
,$connection->stat()
or mysqli_thread_id($connection)
, the three will throw: 'Couldn't fetch mysqli
' if the connection was closed before.
虽然有些人建议检查$connection->ping()
,$connection->stat()
或mysqli_thread_id($connection)
,但Couldn't fetch mysqli
如果连接之前关闭,这三个将抛出: ' '。
The solution that worked for me was:
对我有用的解决方案是:
if(is_resource($connection) && get_resource_type($connection)==='mysql link'){
$connection->close(); //Object oriented style
//mysqli_close($connection); //Procedural style
}
PS: Checking only if it's a resourcecould be enough in a controlled context.
PS:仅在受控上下文中检查它是否是资源就足够了。
回答by Chris Bornhoft
If you open a connection, it will stay open until it's explicitly closed or the script ends (unless persistent connections is on). Using the code you have should work.
如果您打开一个连接,它将保持打开状态,直到它被明确关闭或脚本结束(除非持久连接打开)。使用您拥有的代码应该可以工作。
One option is to extend the mysqli
class and have a status property called $connected
:
一种选择是扩展mysqli
该类并具有一个名为 的状态属性$connected
:
class CustomMysqli {
public $connected;
public function __construct($host, $username, $password, $dbname) { // db info
$conn = new mysqli($host, $username, $password, $dbname);
if (!$conn->connect_errno) {
$this->connected = true;
}
return $conn;
}
public function close($conn) {
if ($this->connected) {
$conn->close();
$this->connected = false;
}
}
}
Checking for the $connected
property is a bit overkill, but will ensure the connection is still open.
检查$connected
属性有点矫枉过正,但会确保连接仍然打开。
回答by Your Common Sense
Just don't close it. That would be the best solution ever.
只是不要关闭它。那将是有史以来最好的解决方案。
99.9% of time it's perfectly ok to leave the connection alone, PHP will close it for you.
99.9% 的情况下完全可以不理会连接,PHP 会为您关闭它。
Only if your script has to perform some heavy time consuming task that doesn't involve a database interaction, you may want to close the connection before starting this operation. But in this case you will have the connection deliberately open as there will be no random closures scattered around the code and therefore will be no need to check whether it is closed already. Hence, just close it, in this particular but extremely rare case.
只有当您的脚本必须执行一些不涉及数据库交互的繁重耗时的任务时,您才可能需要在开始此操作之前关闭连接。但是在这种情况下,您将有意打开连接,因为代码周围不会散布随机闭包,因此无需检查它是否已经关闭。因此,在这种特殊但极为罕见的情况下,只需关闭它。
All other time just leave it alone.
其他时间就别管它了。
回答by hamid
Check connection errors. mysqli_connect() always returns a MySQLi object.
检查连接错误。mysqli_connect() 总是返回一个 MySQLi 对象。
use this:
用这个:
$mysqli_connection = new MySQLi('localhost', 'user', 'pass', 'db');
if ($mysqli_connection->connect_error) {
echo "Not connected, error: " . $mysqli_connection->connect_error;
}
else
{
echo "Connected.";
}
回答by Hazem_M
Try this:
尝试这个:
function close_connection(){
$thread = $mysqli->thread_id;
$mysqli->close();
$mysqli->kill($thread);
}
That will close the connection you opened using object oriented style like this:
这将关闭您使用面向对象的样式打开的连接,如下所示:
$mysqli = new mysqli($db_server, $db_username, $db_password, $db_name);
That's the basic idea, but if you're using the procedural style, I'm sure you'll be able to custom the code as you need.
这是基本思想,但如果您使用程序风格,我相信您将能够根据需要自定义代码。