|
mysql_close
Close MySQL connection
(PHP 4, PHP 5, PECL mysql:1.0)
Example 1414. mysql_close() example<?php The above example will output: Connected successfully Related Examples ( Source code ) » mysql_close Examples ( Source code ) » Open browser password dialog and authenticate user based on database Examples ( Source code ) » Close database connections Examples ( Source code ) » Table data insert Examples ( Source code ) » Adding a Row to a Table Examples ( Source code ) » List Database, Table, and Field Examples ( Source code ) » Listing All Rows and Fields in a Table Examples ( Source code ) » Finding the Number of Rows Returned by a SELECT Statement with mysql_num_rows() Examples ( Source code ) » Listing the Databases Available in a Connection Examples ( Source code ) » Opening a Connection and Selecting a Database Examples ( Source code ) » Stopping Page Execution Examples ( Source code ) » Loading table data from a CSV file Code Examples / Notes » mysql_closejonbarnett
RonS is mostly incorrect about mysql_close. It's not necessarily good programming practice to always explicitly close your database connect as it might be in other languages. Your connection will be closed as soon as your script is finished, so there is almost never a need to call mysql_close at the end of a script. If you have a script that does a lot of queries, it's dangerous to overuse the mysql_close function, as constantly opening and closing a connection can cause a lot of overhead, especially if you're connecting to a remote database. If you are already connected to a database and try to use mysql_connect again on the same database, then the same connection will be returned, and no new connection will be opened. So, it's safe to overuse mysql_connect without overusing mysql_close. If you need to open multiple connections to the same database, then create two connections with mysql_connect with the extra boolean argument to force a new connection to be made without reusing the old connection. The only compelling reason for using mysql_close is if your script is assuredly finished with the connection, but still has a lot of processing left to do. It is almost never beneficial to open and close the same connection throughout a script. 4dacres
Read http://bugs.php.net/bug.php?id=30525, and wow... It might be 'just how it works', but this reads like a broken implementation. cameron2 has it right, with this implementation cleaning up connections with mysql_close() can never be done, esp. if the connection is passed to a routine that you have no visibilty into. You just never know if setting the variable to null or calling mysql_close() is going to error the script.
rons
I just got caught with the "connection to the same database" issue. I need to do a lookup to a master database for syncing issues, but there was a possibility a conenction to the master could be open elsewhere. When I closed "my connection" it closed the "other connection" as well. My goodness, this violates every principle in modern programming technique. All functions can't possibly be smart enough to know if there is an existing connection to a database, and who the user is and all other parameters. It is, however, good programming practice to close your database connections after you're done with them, a practice that I'll have to avoid allowing the program's termination to close the db since I can't tell if the return from mysql_connect() has given me my own resource or a pre-existing one. Absolutely preposterous. Since this is the lowest-level access to the database in the language, it should do as requested. beer_nomaed _at_ hotmail _dot_ com
Be careful when using multiple links to connect to same database (with same username). Unless you specify explicitly in mysql_connect() to create a new link, it will return an already open link. If that would be closed by mysql_close(), it will also (obviously) close the other connection, since the link is the same. Had lot of trouble figuring it out, since in <=4.3.6 there was a bug which didn't close the connection, but after the patch to >=4.3.7, all my application broke down because of a single script that did this. levi
As at 5.0.x and 4.3.x: This function should never be used with shared links; instead you should set your link variables to null. (This explains red's and beer's () problems in previous comments) Here is how shared links work: - Each link is a resource. mysql_connect() by default looks for a resource with the same paramaters. If one exists, it will return the existing resource. - Every assignment of that resource to a variable increases the resource's reference count. - When the reference is decremented to zero, the underlying TCP/socket connection is closed. - Every assignment of a variable away from that resource decrements the reference count. (This includes a function level variable going out of scope) - mysql_close() also decrements the reference count. Note the last two points: mysql_close() _and_ reassignment of a variable decrement the link's reference count. A common mistake is a function like: <? function dothings() { $link = mysql_open(...); .. do some queries .. mysql_close($link) $link = null; } ?> this will decrement the counter twice, possibly closing the underlying connection and causing errors in other parts of the program. http://bugs.php.net/bug.php?id=30525 "this is not a bug but just how it works" bbodelcampo
A little note about multiple simultaneous connections to different hosts... I work on a site that pulls content primarily from one db but uses a db on a foreign server to verify licensing. One might expect the following to work: // Open the connection to the primary db $res1 = mysql_connect($host1, $user1, $pass1); mysql_select_db($db1); // Open connection to the license server $res2 = mysql_connect($host2, $user2, $pass2); mysql_select_db($db2, $res2); // Pull license data and close when done mysql_query($check_sql, $res2); ... mysql_close($res2); // Now pull content from the primary db // Not specifying the resource should default to the last open db mysql_query($query); ... Turns out this last query, since it cant find an active connection, will try to connect with mysql_connect() with no paramaters. But if instead you do it as mysql_query($query, $res1), or alternatively, run the mysql_connect for this host again then it works fine. Thus, it doesnt seem to be possible to have code with an overarching "global" db connection interspersed with temporary connections to another host/db.... |
Change Languagemysql_affected_rows mysql_change_user mysql_client_encoding mysql_close mysql_connect mysql_create_db mysql_data_seek mysql_db_name mysql_db_query mysql_drop_db mysql_errno mysql_error mysql_escape_string mysql_fetch_array mysql_fetch_assoc mysql_fetch_field mysql_fetch_lengths mysql_fetch_object mysql_fetch_row mysql_field_flags mysql_field_len mysql_field_name mysql_field_seek mysql_field_table mysql_field_type mysql_free_result mysql_get_client_info mysql_get_host_info mysql_get_proto_info mysql_get_server_info mysql_info mysql_insert_id mysql_list_dbs mysql_list_fields mysql_list_processes mysql_list_tables mysql_num_fields mysql_num_rows mysql_pconnect mysql_ping mysql_query mysql_real_escape_string mysql_result mysql_select_db mysql_set_charset mysql_stat mysql_tablename mysql_thread_id mysql_unbuffered_query |