Forum Moderators: coopster

Message Too Old, No Replies

mysql_close()

Invalid MySQL Link Resource Warning

         

createErrorMsg

3:37 pm on Nov 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a script that opens a connection to the mySQl database and runs three queries in a row. The first SELECTs data from a table. The second INSERTs that data into another table. The third DELETEs that data from the first table.

The script works fine up to this point, but throws up this warning when I try to close the connection:

Warning: mysql_close(): 14 is not a valid MySQL-Link resource...

From what I have been able to find, this warning is a result of there not being a connection open to the database, but clearly this isn't true since the three queries to the database all run error/warning free.

Here's a simplified version of the code:

$conn = get_connected(); //db connect and select function
$query1 = "SELECT * FROM $post_type WHERE id=$id";
$result1 = mysql_query($query1) or die(mysql_error());
$row1 = mysql_fetch_assoc($result1);
//use $row1
$query2 = "INSERT INTO backup_delete (cols) VALUES ('$vals')";
$result2 = mysql_query($query2) or die(mysql_error());
$rows2 = mysql_affected_rows();
if ($rows2 == 1) {
$query3 = "DELETE FROM $post_type WHERE id=$id";
$result3 = mysql_query($query3) or die(mysql_error());
$rows3 = mysql_affected_rows();
if ($rows3 == 1) {
include('delete.php');
} else {
print("Unable to backup entry. Delete aborted. Please try again.");
}
//close the database connection
mysql_close($conn);

So the question is...is mySQL automatically closing my connection after the third query?

If I remove the mysql_close() from the script it runs fine and error free, but I don't really like the idea of leaving the connection open.

I really appreciate any insight you might have.

Thanks,
cEM

Robber

4:50 pm on Nov 26, 2004 (gmt 0)

10+ Year Member



The mysql connection will be closed automatically once the script has finished running so you don't really need it.

However, if you really want to close it explicitly you might need to consider this line from php.net:

Note: mysql_close() will not close persistent links created by mysql_pconnect().

createErrorMsg

9:15 pm on Nov 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, RObber.

Moments before seeing your post I read the same thing in the PHP manual. If it's really not necessary, I'll just leave it out.

Appreciate the help!
cEM

Salsa

12:28 am on Nov 27, 2004 (gmt 0)

10+ Year Member



Robber is right that the connection will be automatically closed when the sript ends, but I'd still be curious about this behavior. I'm wondering if you have a call to mysql_close($conn) in your delete.php include file. If so, that might explain it.

createErrorMsg

8:58 pm on Nov 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm wondering if you have a call to mysql_close($conn) in your delete.php include file.

Nice, Salsa! You got it. I checked it out and delete.php calls a function that builds a list from the database and then closes the connection. I never would have traced it down; you've got sharp eyes (instincts? something).

Thanks. Knowing the origin of the problem, is the suggestion still to leave it out and let the connection close itself, or should I explicitly close it in the function?

cEM

jollymcfats

11:57 pm on Nov 27, 2004 (gmt 0)

10+ Year Member



I wouldn't close the connection in the function.

I personally use an approach where my

get_connected();
function only connects once, no matter how many times it is called. (via global variables,
static
variables, whatever you like.)

All of my functions that need db connectivity just call

get_connected();
to get a database handle and don't worry about closing it. As noted, it is closed when the script exits, and if you're sharing a connection, other functions will be suprised to find it suddenly closed... You could always use a
auto_append
file to add closing logic to every page, if you needed to.

I do, however, always free my MySQL results with mysql_free_result in the function that creates them.

Salsa

4:36 am on Nov 28, 2004 (gmt 0)

10+ Year Member



My opinion is that it depends on how tidy and anal you want to be. Personally, I tend to be more on the tidy (rather than the anal side ;), and close my connections. But, knowing that delete.php closes the connection in this case you can pretend that, "I meant to do that!" and, obviously, not close it again. The short story, though, is that I think it doesn't really matter whether you call mysql_close unless you are running a really long script that makes multiple calls to databases on various hosts and you need to keep identifiers straight--or to free the minimal resources used. In the end, I tend to focus more on calling mysql_connect a minimal number of times so that PHP doesn't have to take the time to think, "Duh, I've already done that."