We help IT Professionals succeed at work.
Get Started

PHP Loop with testing against value of a query

360 Views
Last Modified: 2012-05-12
I am new to php.  I am trying to run a loop 5 times.  Each time the loop runs I need to run a batch file to refresh a table and test the value of a query.  If the query is > 0 (meaning there are records in the table) exit the loop. Otherwise run a different batch file and continue.  

Start Loop
Run an exec(batchfile)
SELECT     COUNT(item_code) AS ItemCount
FROM         items
ItemCount > 0
       yes - exit loop and continue with code
      No
      Repeat up to 5 times
If after 5 times, exit loop and run exec(RunRefresh2.bat)
Continue wtih code

A friend helped with some code below but I don't think it is doing exactly what I am trying to do

I don't know which type of loop to run the necessary syntax.  Can anyone help?
exec('c:\Inetpub\wwwroot\RunRefresh.bat');
$i=0;
while($i<5)
{
	$sql="SELECT COUNT(item_code) AS ItemCount FROM items";
	$stmt = sqlsrv_query( $conn, $sql);
	$rows=sqlsrv_fetch_object($stmt);
	if($rows->ItemCount<=0)
	{
	exec('c:\Inetpub\wwwroot\RunRefresh2.bat');
		sleep(20);
	}
	else
	{
		break;
	}
	$i++;	
}

Open in new window

Comment
Watch Question
Freelancer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE