Is this code is efficient?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
phazorRise
Forum Contributor
Posts: 134
Joined: Mon Dec 27, 2010 7:58 am

Is this code is efficient?

Post by phazorRise »

Hey y'all,
I have a table consisting of records that are many in numbers. And I want to display all the records.
As number of records are more, displaying on single page is not a good way to go to.
So I wrote this code that displays records pagewise.

Code: Select all

 <?php
mysql_connect("localhost","phazorRise","password")or die("Couldn't make connection");
if (! @mysql_select_db("global") ) {
		echo( "<P>Unable to locate the  " ."database at this time.</P>" );
		exit();								}
		
		$result=mysql_query("select text from test");
		if (!$result) {
		echo("<P>Error performing query: " .
		mysql_error() . "</P>");
		exit();			}
		$no_of_rows=(int) mysql_num_rows($result);
		
		

if(isset($stop))
{
	$start=disp($start,$stop,$no_of_rows,$start2,$stop2);	
	
}
else
{
	$start=disp(0,3,$no_of_rows,$start2,$stop2);
}
	function disp($start,$stop,$no_of_rows,$start2,$stop2)
	{
		if (! @mysql_select_db("global") ) {
		echo( "<P>Unable to locate the joke " ."database at this time.</P>" );
		exit();								}
		
		$result=mysql_query("select text from test");
		if (!$result) {
		echo("<P>Error performing query: " .
		mysql_error() . "</P>");
		exit();			}
		
		while($start<$stop) 
		{
			
			if($start<$no_of_rows)
			{			
			$check=mysql_data_seek ($result,$start);
			if(!$check){
			echo("<P>Error performing query: " .
			mysql_error() . "</P>");
			exit();		}
			
			$final=mysql_fetch_array($result);
			if(!$final){
			echo("<P>Error performing query: " .
			mysql_error() . "</P>");
			exit();		}
			
			print $start.$final["text"].'<br>';			
			$start++;
			}
		}
    	return $start;
	}	
	$stop=$stop+6;
$start2=$start-3;
$stop2=$stop-3;
if($start2==0 && $stop2==3)
{
echo("<u>Previous</u>"); print '&nbsp;&nbsp;'; echo("<A HREF='$PHP_SELF?stop=$stop&&start=$start'>" . "Next</A>"); 
}
else
{
echo("<A HREF='$PHP_SELF?stop2=$stop2&&start2=$start2'>" . "Previous</A>"); print '&nbsp;&nbsp;'; echo("<A HREF='$PHP_SELF?stop=$stop&&start=$start'>" . "Next</A>"); 
}
?>
$start and $stop are passed to display next 3 records while $start2 and $stop2 are passed to display previous 3 records.

No of records on single page are decided as 3.

Thank you. I appreciate your help.
User avatar
phazorRise
Forum Contributor
Posts: 134
Joined: Mon Dec 27, 2010 7:58 am

Re: Is this code is efficient?

Post by phazorRise »

plz someone tell me is above code is efficient???
i want to use it but before i want to confirm.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Is this code is efficient?

Post by VladSun »

No, it's not. In fact, it's very inefficient.

Take a look at LIMIT and OFFSET MySQL keywords.

Also, google for PHP pagination class
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
phazorRise
Forum Contributor
Posts: 134
Joined: Mon Dec 27, 2010 7:58 am

Re: Is this code is efficient?

Post by phazorRise »

VladSun wrote:No, it's not. In fact, it's very inefficient.

Take a look at LIMIT and OFFSET MySQL keywords.

Also, google for PHP pagination class

thank you. i spent hours writting this code. but hell, it's of no use !
Post Reply