This topic is locked
[SOLVED]

 WHILE SQL Syntax

4/9/2010 11:51:07 AM
PHPRunner General questions
K
Khris author

Would someone be able to check my code and tell me what I'm missing? The query runs fine and it does what it's supposed to, but it doesn't finish properly. Essentially it keeps processing and eventually I get a PHP timeout error.

global $conn;

$strSQLSelect1 = "SELECT READERID, PANELID FROM dbo.Reader2";

$rsSelect1 = db_query($strSQLSelect1,$conn);
WHILE ($fetchdata1 = db_fetch_array($rsSelect1))

{

$strSQLSelect2 = "SELECT READERID, PANELID FROM dbo.READER WHERE READERID = ".$fetchdata1["READERID"]." AND PANELID = ".$fetchdata1["PANELID"]."";

$rsSelect2 = db_query($strSQLSelect2,$conn);
WHILE ($fetchdata2 = db_fetch_array($rsSelect2))

{

IF ($fetchdata2["READERID"] == '' AND $fetchdata2["PANELID"] == '')

{

$strSQLUpdate = "UPDATE dbo.Reader2 SET Active = 0 WHERE READERID = ".$fetchdata1["READERID"]." AND PANELID = ".$fetchdata1["PANELID"]."";

$rsUpdate = db_query($strSQLUpdate,$conn);

}

ELSE

{

$strSQLUpdate = "UPDATE dbo.Reader2 SET Active = 1 WHERE READERID = ".$fetchdata1["READERID"]." AND PANELID = ".$fetchdata1["PANELID"]."";

$rsUpdate = db_query($strSQLUpdate,$conn);

}

}

}
A
alang 4/11/2010

Your syntax looks OK to me but I am having a bit of difficulty understanding the logic.
Firstly from what I can see you have queries that are nested three deep so the first thing is that depending on how large your tables are and whether you have indexes on the READERID and PANELID fields, it is conceivable that the whole process could take a long time leading to the timeout error (you can change this on your server if you have access - I think it is normally 30sec by default).
Secondly you have a test in the inner loop on READERID and PANELID being blank but the second level query only pulls records where both of these values are the same as the ones in the first table so the first part of the IF statement will only be executed in the case where READERID and PANELID are blank in Reader2.
Thirdly, I have a feeling that you may be better off trying to do the process in one SQL update query using subqueries if necessary. If you confirm what you want to do, with a bit more info on the tables, someone might be able to add more help.

K
Khris author 4/12/2010

dbo.READER is a view in my database which is linked to another database. No modification of this data is allowed.

dbo.Reader2 is essentially a copy of dbo.Reader with some extra information added in to categorize it.
Every now and again, an external source will remove information from dbo.READER. My query above is designed to compare the information in dbo.Reader2 back to dbo.READER. If it finds information in dbo.Reader2 that doesn't match dbo.READER, it flags it as Inactive (0) so it can be removed.
I agree, a nested SQL statement would probably be better, but I haven't quite got my head wrapped around that yet.
There are about 773 records in total but I've verified that all records are processed before the PHP query timeout value (which I've set to 300 seconds for testing). The query seems to hang without exiting properly however.