File: 0.04.15b/server/base/tests/mysql.inc.php (View as HTML)

  1: <?php // mysql.inc.php -- MySQL Test
  2: /* -------------------------------------------------------------
  3: This file is part of FreeNATS
  4: 
  5: FreeNATS is (C) Copyright 2008 PurplePixie Systems
  6: 
  7: FreeNATS is free software: you can redistribute it and/or modify
  8: it under the terms of the GNU General Public License as published by
  9: the Free Software Foundation, either version 3 of the License, or
 10: (at your option) any later version.
 11: 
 12: FreeNATS is distributed in the hope that it will be useful,
 13: but WITHOUT ANY WARRANTY; without even the implied warranty of
 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15: GNU General Public License for more details.
 16: 
 17: You should have received a copy of the GNU General Public License
 18: along with FreeNATS.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freenats
 21: -------------------------------------------------------------- */
 22: 
 23: function mysql_test_rows($host,$user,$pass,$database="",$timeout=0,$query="",$debug=false)
 24: {
 25: global $NATS;
 26: if ($timeout>0) $timeout=$timeout; // use specific for test if set
 27: else
 28: 	{
 29: 	// otherwise use system if available
 30: 	if (isset($NATS)) $timeout=$NATS->Cfg->Get("test.mysql.timeout",0);
 31: 	if ($timeout<=0) $timeout=0; // unset specifically or in environment
 32: 	}
 33: // this will return a 0 at any stage if connect etc is ok but no rows are returned
 34: // negative if something actually fails
 35: if ($timeout>0)
 36: 	{
 37: 	$oldtimeout=get_ini("mysql.timeout");
 38: 	set_ini("mysql.timeout",$timeout);
 39: 	}
 40: 
 41: if ($debug) echo "mysql://".$user.":".$pass."@".$host."/".$database."\n";
 42: 
 43: $sql=@mysql_connect($host,$user,$pass,true);
 44: 
 45: if ((!$sql)||($database==""))
 46: 	{
 47: 	if ($timeout>0) set_ini("mysql.timeout",$oldtimeout);
 48: 	if (!$sql) 
 49: 		{
 50: 		if ($debug) echo "Connect Error: ".mysql_error($sql)."\n";
 51: 		return -1; // total connect failed
 52: 		}
 53: 	// otherwise is no database so close and return 0
 54: 	@mysql_close($sql);
 55: 	return 0;
 56: 	}
 57: 
 58: @mysql_select_db($database,$sql);
 59: 
 60: if (mysql_errno($sql)!=0) // failed to select DB
 61: 	{
 62: 	if ($timeout>0) set_ini("mysql.timeout",$oldtimeout);
 63: 	@mysql_close($sql);
 64: 	return -2;	// select database failed
 65: 	}
 66: 
 67: if ($query=="")
 68: 	{ // no query to perform
 69: 	if ($timeout>0) set_ini("mysql.timeout",$oldtimeout);	
 70: 	@mysql_close($sql);
 71: 	return 0; // all ok but no query/rows
 72: 	}
 73: 	
 74: $r=@mysql_query($query,$sql);
 75: if (mysql_errno($sql)==0) // successful query
 76: 	{
 77: 	if (is_bool($r)) // didn't return any daya
 78: 		{
 79: 		$return=mysql_affected_rows($sql);
 80: 		}
 81: 	else 
 82: 		{
 83: 		$return=mysql_num_rows($r);
 84: 		@mysql_free_result($r,$sql); // free if a result
 85: 		}
 86: 	}
 87: else $return=-3; // query failed
 88: 
 89: @mysql_close($sql);
 90: if ($timeout>0) set_ini("mysql.timeout",$oldtimeout);
 91: return $return;
 92: }
 93: 
 94: function mysql_test_time($host,$user,$pass,$database="",$timeout=0,$query="",$debug=false)
 95: {
 96: $timer=new TFNTimer();
 97: $timer->Start();
 98: $val=mysql_test_rows($host,$user,$pass,$database,$timeout,$query,$debug);
 99: $time=$timer->Stop();
100: 
101: if ($val<0) return $val; // connect/select/query failed
102: 
103: // if $val is 0 then nothing was returned - maybe check the query here? Complicates the idea
104: // though for the user so left. Will have to do two tests for both time and rows 0 as fails
105: 
106: $time=round($time,4);
107: if ($time==0) return "0.0001";
108: return $time;
109: }
110: 
111: ?>