PHP MySQL class - what am I doing wrong?

Associate
Joined
7 Jan 2005
Posts
1,026
Ok, don't get me wrong, my code actually works, I'm just wondering why the MySQL class I made benchmarks as faster than doing it WITHOUT the class!

Here's the code and how I'm testing it.
The Class inc_mysqlclass3.php
PHP:
<?
// *** MySQL Query Class

class mysql_base{
	protected $existingstring = FALSE;
	function __construct($host, $username, $password, $database) {
		mysql_connect($host, $username, $password) or die(mysql_error());
		mysql_select_db($database) or die(mysql_error());
	}
	function query($sql) {
		if(!$this->existingstring) {
			$this->existingstring= TRUE;
			$this->query = mysql_query($sql);
		}
		if($returnthis = mysql_fetch_assoc($this->query)) {
			return
			$returnthis;
		}else{
			$this->existingstring = FALSE;
			return FALSE;
		}
	}
	
}
The Benchmark index.php
PHP:
<?
 function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html;
		charset=iso-8859-1" />
		<title>OOP in PHP</title>
	</head>
	<body>
	<?php include("inc_mysqlclass3.php"); ?>
		<?php  
		$time_start = microtime_float(); 
		
		//'#### start of new class
			$mysqlconn = new mysql_base('localhost','Myvaya_wpuser','password','Myvaya_blog');
			for($i=0;$i<1000;$i++){
				while($row = $mysqlconn->query('SELECT * FROM tkwp_links')) {
					echo('<!--'.$row['link_url'].'<br/>-->');
				}
			}
			mysql_close;
			
		// '### end of new class
		
		$time_end = microtime_float(); 
		$MyTime = $time_end-$time_start;
		echo('<br/>My time:'.$MyTime.'ms<br/>');
		usleep(1000);
		$time_start = microtime_float(); 
		
		//start of old class
		mysql_connect('localhost','Myvaya_wpuser','password') or die(mysql_error());
		for($i=0;$i<1000;$i++){
			$query = mysql_query('SELECT * FROM tkwp_links');
			while($row = mysql_fetch_assoc($query)) {
				echo('<!--'.$row['link_url'].'<br/>-->');
			}
		}
		mysql_close;
		
		// end of old class
		$time_end = microtime_float(); 
		$PHPTime = $time_end-$time_start;
		echo('<br/>PHP\'s time:'.$PHPTime.'ms<br/>');
		echo('Difference:'.($MyTime-$PHPTime).'ms<br/>');
		
		
		?>
		<!--	$result = $returnrows->query('SELECT * FROM tkwp_links');
			while($row = mysql_fetch_assoc($result)) {
				echo($row['link_url']."<br/>");
			}
		  -->
	 </body>
</html>

It doesn't make any sense to me that doing it the class way is between 0.2 and 0.4 seconds FASTER than doing it the regular way.

Only explanation is that my benchmark's not doing it on the same terms...
What am I doing wrong?
 
Err... nevermind... realised why...
I wasn't doing
PHP:
mysql_select_db();
On the second one, so presumably it was querying what DBs the user had access to first... Now it's /generally/ slower, but it seems to swap between faster and slower enough times for it to be relatively close...

Any comments on the class incidentally?
 
Back
Top Bottom