mysql_ping
(PHP 4 >= 4.3.0, PHP 5, PECL mysql:1.0)
mysql_ping — Проверяет соединение с сервером и пересоединяется при необходимости
Описание
bool mysql_ping
([ resource $link_identifier
] )
mysql_ping() проверяет соединение с сервером. Если оно утеряно, автоматически предпринимается попытка пересоединения. Эта функция может быть использована в скриптах, работающих на протяжении долгого времени. mysql_ping() возвращает TRUE, если соединение в рабочем состоянии и FALSE в противном случае.
См. также mysql_thread_id() и mysql_list_processes().
- 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
Коментарии
Is important to remember that if your first connection to mysql don't works, mysql_ping will always return true! So, if you want to check if mysql is connected, first of all you must check if mysql_connect do not returns false and then you can begin to check mysql_ping.
It should be noted that mysql_ping() seems to reset the error message on the server.
I used it to check whether the connection was still alive before reading the error message via mysql_error() and it always returned an empty string. Upon removing the connection check everything worked.
When using the mysql_ping command under php 5.1.2 and mysql 5.0, I was having problems with the auto-reconnect "feature", mainly that when the connection was severed, a mysql_ping would not automatically re-establish the connection to the database.
The connection to the DB is dropped when the time without a query excedes the wait_timeout value in my.cnf. You can check your wait_timeout by running the query "SHOW VARIABLES;"
If you're having problems auto-reconnecting when the connection is dropped, use this code:
<?php
$conn = mysql_connect('localhost','user','pass');
mysql_select_db('db',$conn);
if (!mysql_ping ($conn)) {
//here is the major trick, you have to close the connection (even though its not currently working) for it to recreate properly.
mysql_close($conn);
$conn = mysql_connect('localhost','user','pass');
mysql_select_db('db',$conn);
}
//run queries knowing that your connection is alive....
?>
When checking if a $resource works...
be prepared that mysql_ping returns NULL as long as $resource is no correct mysql resource.
<?php
$resource =NULL;
var_dump = @mysql_ping($resource);
# showing NULL
?>
This could be used to decide of a current $resource is a mysql or a mysqli connection when nothing else is available to do that...
mysql_ping() is really helpful when you have this annoying error:
MYSQL Error 2006 Server has gone away
For CI users:
In 1.7.2 version of codeigniter, there is a function
$this->db->reconnect()
that uses mysql_ping() to reestablish the timed out connection.
This function is specially useful when developing social media sites that uses hundreds of connections to the db such asinserting or selecting.
This function *does not* attempt to reconnect at this time. It only tells you whether or not you currently *are* connected.
To actually reconnect, you will have to implement this yourself in a wrapper class.