Reputation: 2497
I am implementing a script in php for a Wordpress blog. The script should be executed every five minutes. The script opens a mysql connection and I want to close it when I am finished. Can you check if it works?
$db = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db("XY", $db);
//set data into $data
foreach ($data => $info) {
//do stuff inserts
}
mysql_close();
Is the approach right? I am closing the connection used in this script or I am closing also other connections?
Upvotes: 0
Views: 622
Reputation: 2488
Closing the connection explicitly is not mandatory. PHP cleans up all the resources when the script finishes, this includes database connections or sockets, filehandles, any internal resources like images and last but not least, memory blocks.
Upvotes: 3
Reputation: 492
You could pass your $db variable to the mysql_close() function call. Like mysql_close($db).
But be aware that the mysql_* are deprecated and will be removed in a future php version. You should look into the mysqli_* functions instead.
Upvotes: 1