0

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?

7
  • 1
    Any real reason to close connection explicitly? Commented Jan 21, 2011 at 13:27
  • 2
    Any real reason not to use mysqli? Commented Jan 21, 2011 at 13:27
  • always been a bit hazy on when to close connection, so no, not a real reason :) Commented Jan 21, 2011 at 13:28
  • 1
    Any real reason to use mysqli if it is PDO? Commented Jan 21, 2011 at 13:28
  • why are people emphasising real :) but not any particular reason, is mysqli better? Commented Jan 21, 2011 at 13:28

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.