Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
 
LinkBack Thread Tools Display Modes
Old 25-04-2011, 03:02 PM   #1 (permalink)
Mpe
Right Off the Assembly Line
 
Join Date: Mar 2011
Posts: 11
Default PHPMySQL Looping SELECT, is it possible?


i extract years from mysql using the array loop like this,

foreach (array(date('Y', strtotime($row['drilling_date']))) as $v) {
//1988, 1989, 1990 and so on..
$current_year = $v;
$next_year = $v+1; // which shows the year nest to $current_year
// problem here
}

Is there any way i could loop the select to query between years with one query
i have this right now but its not working..

foreach (array("SELECT COUNT(result) FROM hptw WHERE drilling_date between '$v-04-01' AND '$next-03-01' and result = 'success'") as $b);

What i am trying to do is that put the COUNT result in array, the first array would contain the result of 1988 and 1989, the second array, 1989-1990 ... and the third array 1990-1991 an continue looping till it fetch all the year present in the database..

e.g (45(between 1988 - 1989), 50(between 1989-1990) and so on.......)

I hope there is another approach to solve this..

oh i also get this result

Resource id #6

Last edited by Mpe; 25-04-2011 at 03:11 PM.
Mpe is offline   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 25-04-2011, 03:18 PM   #2 (permalink)
Super Moderator
 
asingh's Avatar
 
Join Date: May 2008
Location: New Delhi
Posts: 5,548
Default Re: PHPMySQL Looping SELECT, is it possible?

It would probably need to be a stored proc. since you would use T-SQL.

1. You would need to get the max and min years dynamically.
2. There create a dynamic query to pull out counts.
3. Store them in a table.
4. Use the table storage to get it into your PHP table.

Quote:
CREATE PROC [dbo].[mytestDB]


AS

BEGIN


DECLARE @TABLE_COUNT_MIN int
DECLARE @TABLE_COUNT_MAX int


/*GET THE MAX COUNT'*/
SELECT @TABLE_COUNT_MAX = MAX(YEAR) FROM tbl_tmp_table_info

/*SET THE MIN COUNT*/
SELECT @TABLE_COUNT_MIN = MIN(YEAR) FROM tbl_tmp_table_info

/*LOOP BEGIN*/


WHILE @TABLE_COUNT_MIN !> @TABLE_COUNT_MAX
BEGIN

SELECT @DYN_UPDATE_FIELD = Column_Name FROM tbl_tmp_table_info
WHERE Row_key = CAST(@TABLE_COUNT_MIN AS CHAR)

/*CHANGE THE NULLS */
SET @DYN_UPDATE_QUERY=
'UPDATE tbl_raw_data SET '
+@DYN_UPDATE_FIELD+ ' = ' +@CONST_DUMMY_VAL+
' WHERE '+@DYN_UPDATE_FIELD+ ' IS NULL'

EXEC (@DYN_UPDATE_QUERY)

SET @TABLE_COUNT_MIN = @TABLE_COUNT_MIN + 1

END /*LOOP END*/


END
See how the max and min is first selected and used in a loop. I am doing an update , but you would do an insert to a fresh blank table.
__________________
MSI P45 Platinum(BIOS v1.7B)|Q9550[E0]@3.85Ghz@1.320V[453x8.5]MCH@1.184V|ICH@1.55V|DDR_V_Ref_A_B@1.05V|NH-D14|Corsair TWIN2X4096-8500C5(5-5-5-15)@1089Mhz@2.14V
2xHD4890[Xfire]@1000/900[MEM/GPU]|Corsair 650TX|Seagate180GB+80GB+WD1TB|SONY-DVD-R|CM690|2x120mm Scythe Ultra Kaze|DELL S2409W|APC 1100VA|Scythe Kaze Server
Windows 7 Ultimate RTM - 64BIT|Catalyst 10.5 (8.14.10.0753) forced with RadeonPRO|PS3 160GB|Sony 40EX520|AC Ryan POHD Mini|APC 800VA|APC 800VA|D425KT|CM100 Elite|2TB WD|Acer D255

Test your spoiler tags before submitting
asingh is online now   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:26 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2