简体   繁体   English

PHP错误关闭mysql连接

[英]PHP error closing a mysql connection

I'm using a very simple function: 我正在使用一个非常简单的功能:

function closeConn(){
    mysql_close($conn);
}

$conn is the connection variable - it connects ok but i get this error if i try and call it: $conn是连接变量-它连接正常,但是如果我尝试调用它,则会收到此错误:

Warning: mysql_close() expects parameter 1 to be resource, null given in 警告:mysql_close()期望参数1为资源,在中给出null

What is the reason for this? 这是什么原因呢?

The reason is, $conn variable is empty. 原因是$conn变量为空。

Either pass it as an argument to your function: 要么将其作为参数传递给函数:

function closeConn($conn){
    mysql_close($conn);
}

closeConn($conn);

or just don't use it at all and let PHP decide which connection to close (by default, tha last one that was opened) 或者根本不使用它,而让PHP决定关闭哪个连接(默认情况下,最后一个打开的连接)

function closeConn(){
    mysql_close();
}

or just don't close the connection at all. 或根本不关闭连接。 PHP does it for you anyway, when the script's execution ends. 当脚本执行结束时,PHP仍会为您执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM