note 56918 added to function.mysql-connect
| From: | contact at sonium dot org | Date: | Mon, 19 Sep 2005 14:12:55 +0000 |
| Subject: | note 56918 added to function.mysql-connect | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-95411@lists.php.net to get a copy of this message | ||
I had to use php4 (without mysql.connect_timeout) but wanted to check the availability of a database
(without having to wait for 30 seconds).
if you use fsockopen(with timeout) and just close the connection after a few attempts the server
will block your client.
this script logs you on to mysql and gets disconnected by the server (access denied) but it
doesn't block the client for future connections:
<?php
$host = "127.0.0.1";
$port = 3306;
$timeout = 5;
echo "trying to connect to $host on port $port\n";
$testcon = @fsockopen ($host, $port, $errno, $errstr, $timeout);
if(!$testcon)
die("database unavailable: $errno: $errstr\n");
else
{
echo "connection successful under $timeout seconds\n";
echo "quit connection\n";
$in = fread($testcon,4096);
echo "server says:$in\n";
//login string
$out = array( 0x26,0x00,0x00,0x01, //packet length 38 bytes (without this 4 byte header // packet
no.1
0x85,0xa6,0x03,0x00,
0x00,0x00,0x00,0x01,
0x08,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,
0x66,0x69,0x66,0x69,0x00, //username 'fifi'
0x00); //no password
foreach($out as $o)
{
fputs($testcon,chr($o),1);
}
$in = fread($testcon,4096);
echo "server says:$in\n";
//logout command
$out = array(0x01,0x00,0x00,0x00,0x01);
foreach($out as $o)
fputs($testcon,chr($o),1);
fclose($testcon);
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.mysql-connect.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56918
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56918&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56918&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56918&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56918&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56918&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+56918&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56918&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56918&report=yes
Search -- http://master.php.net/manage/user-notes.php