简体   繁体   中英

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:

Warning: mysql_close() expects parameter 1 to be resource, null given in

What is the reason for this?

The reason is, $conn variable is empty.

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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