Page 1 of 1

not sure if this possible?

Posted: Mon Nov 22, 2010 5:14 am
by IGGt
I'm not sure if this is actually possible, but I thought I would ask anyway...

Is there a way:

Have a list of MySQL servers;

Connect to first MySQL server;
__IF unable to connect - display an icon;
__IF able to connect - display a different Icon
Repeat with the next server


So what I end up with is a list of icons showing which servers are running and which aren't.

At the moment, the best I can manage is to either divert to a different page with an error message but doesn't give much information about which are OK and which aren't, or display all the PHP errors, and have it stop running at the first server that fails (even if the subsequent servers are OK).

Re: not sure if this possible?

Posted: Mon Nov 22, 2010 6:14 am
by social_experiment
Thinking about it really quickly it seems possible :? Here is what i have in mind: Displaying the image seems easy (in my mind), you could just echo an image to the browser depending on success or failure of the connection. The tougher part would be the 'checking' each of the connection/s. If you have all the connections and placed them in an array you could use a foreach() statement.

Code: Select all

<?php
 $conn1 = @mysql_connect($host, $user, $pass);
 $conn2 = @mysql_connect($host, $user, $pass);
 $conn3 = @mysql_connect($host, $user, $pass);

 $conn_array = array($conn1, $conn2, $conn3);

 foreach ($conn_array as $key) {
  // if the connection is made OR exists
  if ($key) { // display connected icon
  }
  else { // display disconnected icon
  }
?>
That's how i would attempt to solve it.

Re: not sure if this possible?

Posted: Mon Nov 22, 2010 7:19 am
by IGGt
That's kind of what I was thinking.

So far I have a script that checks each slave to see if replication is running, which is very similar to what you wrote, but the problem is as soon as it gets to the first database/server that is switched off it gives an error and stops.

So if database 1,2,3,4 are OK, but then 5 is unreachable, but 6,7,8,9 are OK, it only displays the result for 1,2,3,4.

Re: not sure if this possible?

Posted: Mon Nov 22, 2010 7:36 am
by monstaink
sorry guys i may be well off the mark here as i'm just going through turorials, but with the example that has been given in the reply and what you are saying is that not where the elseif will come into play

if (first condition is true) {
do this!
}
elseif (second condition is true) {
do this!
}
elseif (third condition is true) {
do this!
}
... and so on ...
else {
do this!
}

Would love to know if i'm firing in the right direction will deffo give me a boost of confidence if i am
regards

Re: not sure if this possible?

Posted: Mon Nov 22, 2010 8:15 am
by IGGt
Ok,

I'm getting close. What I have so far is:

Code: Select all

include ('connections_array.php');  

print "<table><tr>";
foreach($connections_array as $db) {
	$dbname = $db['database'];
	$con = mysql_connect($db['server'], $db['user'], $db['password']);

	
			if ( !$con ) {
		
				print "   <td><img src=\"images/red.gif\" alt=\"Database Failed\" title=\"$dbname\" /></td>\n";
			
			} else {
				
				print "   <td><img src=\"images/green.gif\" alt=\"Database OK\" title=\"$dbname\" /></td>\n";
				mysql_close($con);	
				}	
	}
								
print "</tr></table>";
Thi gives me a line of circles, either green or red, and if you hover over them it tells you the name. The only problem I have, is that it still shows the error message:

[text]
Warning: mysql_connect() [function.mysql-connect]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:33071) in C:\wamp\www\ts\error2.php on line 21

Warning: mysql_connect() [function.mysql-connect]: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\wamp\www\ts\error2.php on line 21
[/text]

If I can find a way of hiding that text, i'll be well away.

Re: not sure if this possible?

Posted: Mon Nov 22, 2010 8:28 am
by IGGt
found it, add an @ before the mysql_connect and it hides the output,

in other words:

$con = @mysql_connect($db['server'], $db['user'], $db['password']);

Re: not sure if this possible?

Posted: Mon Nov 22, 2010 8:55 am
by social_experiment
monstaink wrote:sorry guys i may be well off the mark here as i'm just going through turorials, but with the example that has been given in the reply and what you are saying is that not where the elseif will come into play
The idea is not off the mark it's just the syntax. If you did use if / elseif / else you would end up with a lot more code than IGGT's current example. Also, what if you added another 5 server's to check, you'd have to add another 4 elseif statements to your existing code and that would change each time the amount of servers (to check) changes. :)

IGGT: It was a wild guess on my part, glad it works. I tried it with a database tables and it worked a treat, couldn't test the servers option though.

Re: not sure if this possible?

Posted: Mon Nov 22, 2010 9:21 am
by IGGt
As a note, I've just found a glitch.

If you have two databases, one after the other, that have stopped running, the script stops at the first one.

e.g.
db1 - OK
db2 - failed
db3 - OK
db4 - OK
you will get 4 databases returned.

db1 - OK
db2 - failed
db3 - failed
db4 - OK
you will get db1, db2 only returned.

Re: not sure if this possible?

Posted: Mon Nov 22, 2010 11:08 am
by social_experiment
Ok, i did something similar (actually with database instead of tables as i earlier mentioned)

Code: Select all

<?php
mysql_connect('localhost', 'xxx', 'yyy');

// database on my localhost, some real, some non-existant	
$databases = array('advertising', 'blog', 'cdcol', 
'double', 'electric', 'joke', 'joomla', 'test');
	
echo '<table width="10%">';
foreach ($databases as $value) {
	$dbs = mysql_select_db($value);
	if ($dbs) { echo '<tr><td>' .$value .'</td>
	<td><img src="checkmark.gif" width="16" 
	height="16" alt="Selected" title="Selected" /></td></tr>'; }
	else { echo '<tr><td> '. $value .' </td><td><img src="x.gif"
	width="16" height="16" alt="Not Selected" 
	title="Not Selected" /></td></tr>'; }
}
echo '</table>';
 ?>
I see i didn't suppress the error that arises from mysql_select_db() but i didn't receive any :?

What information is inside your 'connections_array.php' file?

Re: not sure if this possible?

Posted: Mon Nov 22, 2010 11:10 am
by IGGt
it looks like:

Code: Select all

<?php
$u = 'xxx';
$p = ''xxx";

$connections_array[] = array('server' => 'localhost:3306',
                             'user' => $u,
                             'password' => $p,
                             'database' => 'MySQL01',
                             'dbID' => '1',
                             'default' => 'test'
                                           );
and so on for each database