Forum Moderators: coopster
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
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
I personally use an approach where my
get_connected();function only connects once, no matter how many times it is called. (via global variables,
staticvariables, 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_appendfile 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.