mysql_close
(PHP 4, PHP 5)
mysql_close — Close MySQL connection
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
- mysqli_close()
- PDO: Assign the value of
NULL
to the PDO object
Description
$link_identifier
= NULL
] )
mysql_close() closes the non-persistent connection to
the MySQL server that's associated with the specified link identifier. If
link_identifier
isn't specified, the last opened
link is used.
Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution. See also freeing resources.
Parameters
-
link_identifier
-
The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no connection is found or established, an
E_WARNING
level error is generated.
Return Values
Returns TRUE
on success or FALSE
on failure.
Examples
Example #1 mysql_close() example
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
The above example will output:
Connected successfully
Notes
Note:
mysql_close() will not close persistent links created by mysql_pconnect().
See Also
- mysql_connect() - Open a connection to a MySQL Server
- mysql_free_result() - Free result memory
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с базами данных
- Расширения для работы с базами данных отдельных производителей
- MySQL Drivers and Plugins
- Оригинальное API MySQL
- mysql_affected_rows
- 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
Коментарии
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:
<?php
// 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....
i just came over a problem that i had with apache.
It crashs and said :
"Parent: child process exited with status 3221225477 -- Restarting."
the error came from the extesion php_mysql.dll
i didn't understand what was the reason of that crash..
Then, i debug the script that i had downloaded and i noticed that that was the function mysql_close() which caused the problem.
The solution is, to send to it the link identifier which is optionnal in the description but cause a crash with no commentary.
Thanks to agneady.