PHP: mysql_ping - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

mysql_query mysql_pconnect

Last updated: Fri, 26 Jan 2007

view this page in

# mysql_ping

(PHP 4 >= 4.3.0, PHP 5)

mysql_ping -- Проверяет соединение с сервером и пересоединяется при необходимости

## Описание

bool mysql_ping ( [resource link_identifier] )

mysql_ping() проверяет соединение с сервером. Если оно утеряно, автоматически предпринимается попытка пересоединения. Эта функция может быть использована в скриптах, работающих на протяжении долгого времени. mysql_ping() возвращает `TRUE`, если соединение в рабочем состоянии и `FALSE` в противном случае.

См. также mysql_thread_id() и mysql_list_processes().

add a note User Contributed Notes
mysql_ping
dustin hawkins
31-Jul-2006 12:32
`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....
?>`
cybot2000 at yahoo dot de
25-May-2005 04:44
`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.`
vinicius at teracom dot com dot br
16-Mar-2004 07:35
`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.`
add a note
mysql_query mysql_pconnect

Last updated: Fri, 26 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 10:36:09 2007 PST