File: 0.04.00a/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: // this will return a 0 at any stage if connect etc is ok but no rows are returned
 26: // negative if something actually fails
 27: if ($timeout>0)
 28: 	{
 29: 	$oldtimeout=get_ini("mysql.timeout");
 30: 	set_ini("mysql.timeout",$timeout);
 31: 	}
 32: 
 33: if ($debug) echo "mysql://".$user.":".$pass."@".$host."/".$database."\n";
 34: 
 35: $sql=@mysql_connect($host,$user,$pass,true);
 36: 
 37: if ((!$sql)||($database==""))
 38: 	{
 39: 	if ($timeout>0) set_ini("mysql.timeout",$oldtimeout);
 40: 	if (!$sql) 
 41: 		{
 42: 		if ($debug) echo "Connect Error: ".mysql_error($sql)."\n";
 43: 		return -1; // total connect failed
 44: 		}
 45: 	// otherwise is no database so close and return 0
 46: 	@mysql_close($sql);
 47: 	return 0;
 48: 	}
 49: 
 50: @mysql_select_db($database,$sql);
 51: 
 52: if (mysql_errno($sql)!=0) // failed to select DB
 53: 	{
 54: 	if ($timeout>0) set_ini("mysql.timeout",$oldtimeout);
 55: 	@mysql_close($sql);
 56: 	return -2;	// select database failed
 57: 	}
 58: 
 59: if ($query=="")
 60: 	{ // no query to perform
 61: 	if ($timeout>0) set_ini("mysql.timeout",$oldtimeout);	
 62: 	@mysql_close($sql);
 63: 	return 0; // all ok but no query/rows
 64: 	}
 65: 	
 66: $r=@mysql_query($query,$sql);
 67: if (mysql_errno($sql)==0) // successful query
 68: 	{
 69: 	if (is_bool($r)) // didn't return any daya
 70: 		{
 71: 		$return=mysql_affected_rows($sql);
 72: 		}
 73: 	else 
 74: 		{
 75: 		$return=mysql_num_rows($r);
 76: 		@mysql_free_result($r,$sql); // free if a result
 77: 		}
 78: 	}
 79: else $return=-3; // query failed
 80: 
 81: @mysql_close($sql);
 82: if ($timeout>0) set_ini("mysql.timeout",$oldtimeout);
 83: return $return;
 84: }
 85: 
 86: function mysql_test_time($host,$user,$pass,$database="",$timeout=0,$query="",$debug=false)
 87: {
 88: $timer=new TFNTimer();
 89: $timer->Start();
 90: $val=mysql_test_rows($host,$user,$pass,$database,$timeout,$query,$debug);
 91: $time=$timer->Stop();
 92: 
 93: if ($val<0) return $val; // connect/select/query failed
 94: 
 95: // if $val is 0 then nothing was returned - maybe check the query here? Complicates the idea
 96: // though for the user so left. Will have to do two tests for both time and rows 0 as fails
 97: 
 98: $time=round($time,4);
 99: if ($time==0) return "0.0001";
100: return $time;
101: }
102: 
103: ?>