CodeCharge Studio
search Register Login  

Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 Check for MySQL Database Availability

Print topic Send  topic

Author Message
datadoit
Posted: 12/01/2008, 5:51 PM

CCS3.2; PHP5; MySQL5

We've got several remote databases that we periodically connect to for
certain information gathering (phone systems, email servers, etc.). I
like to check for the database availability, though, before doing
anything with the connection. The method I'm using is:

$PBX = @mysql_connect('host', 'username', 'password');
if (!$PBX) {
Then don't do what you think you wanted to do;
}

This works fine. However, if the database is not available, then it
takes a long long time. The time it is taking is the MySQL timeout
value (30 seconds).

Does anyone know of a way to perhaps dynamically set the MySQL timeout
value via PHP/CCS (NOT altering the MySQL default settings)? or set the
timeout value per connection? or if there's an entirely different method
for testing database availability via PHP/CCS?
TonyReid


Posts: 159
Posted: 12/03/2008, 6:19 AM

What about this.....

$PBX = @mysql_connect('host', 'username', 'password') or die("Couldn't connect to SQL Server");




_________________
-----------
PHP/indy Game Developer - http://www.AbsoluteBreeze.co.uk
View profile  Send private message
datadoit
Posted: 12/03/2008, 10:08 AM

Thanks Tony, but that doesn't adjust the connect timeout for MySQL,
which for us is set at 30 seconds. I'm looking for a way to dynamically
control that timeout value without changing the MySQL server
configuration value.

Your example will wait the 30 seconds before dieing.

There are some ways of dynamically controlling the timeout value in
other languages, particularly using C. For PHP it's quite limiting for
some reason.

I have found some examples of people successfully using:

ini_set("mysql.connect_timeout","10");
@mysql_connect(...);
ini_restore();

but that's not working in my test environment
(CCS3/PHP5/MySQL5/Apache2/WinXPP). I think it may have something to do
with page buffering (?).
TonyReid


Posts: 159
Posted: 12/04/2008, 2:52 AM

Hmmm.... its instant for me... perhaps its a difference of windows and linux.


_________________
-----------
PHP/indy Game Developer - http://www.AbsoluteBreeze.co.uk
View profile  Send private message
datadoit
Posted: 12/04/2008, 10:33 AM

TonyReid wrote:
> Hmmm.... its instant for me... perhaps its a difference of windows and linux.
>

The die() or the connection? Winderz or Linux?
jjrjr1


Posts: 942
Posted: 12/05/2008, 5:55 AM

Hi Try something like this possibly. Maybe some mods to this will also work

A PHP script for testing a MySQL database connection
A simple page for testing the connection to a MySQL database.
The PHP script will test the server address, username and password.
If the database field is left empty, it will return a list of available databases.
Testing a specific database is optional, but if a database name is supplied,
it will return a list of the tables in that database (if any exist).

<?php

$hostname = trim($_POST['hostname']);
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$database = trim($_POST['database']);

$link = mysql_connect("$hostname", "$username", "$password");
if (!$link) {
echo "<p>Could not connect to the server '" . $hostname . "'</p>\n";
echo mysql_error();
}else{
echo "<p>Successfully connected to the server '" . $hostname . "'</p>\n";
printf("MySQL client info: %s\n", mysql_get_client_info());
// printf("MySQL host info: %s\n", mysql_get_host_info());
}
if ($link && !$database) {
echo "<p>No database name was given. Available databases:</p>\n";
$db_list = mysql_list_dbs($link);
echo "<pre>\n";
while ($row = mysql_fetch_object($db_list)) {
echo $row->Database . "\n";
}
echo "</pre>\n";
}
if ($database) {
$dbcheck = mysql_select_db("$database");
if (!$dbcheck) {
echo mysql_error();
}else{
echo "<p>Successfully connected to the database '" . $database . "'</p>\n";
// Check tables
$sql = "SHOW TABLES FROM $database";
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0) {
echo "<p>Available tables:</p>\n";
echo "<pre>\n";
while ($row = mysql_fetch_row($result)) {
echo "{$row[0]}\n";
}
echo "</pre>\n";
} else {
echo "<p>The database '" . $database . "' contains no tables.</p>\n";
echo mysql_error();
}
}
}

?>
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Internet Database

Visually create Web enabled database applications in minutes.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.