vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   How Do I: Recycle $db->fetch_array($qry) ? (https://vborg.vbsupport.ru/showthread.php?t=124947)

DigitalCrowd 08-26-2006 02:01 AM

How Do I: Recycle $db->fetch_array($qry) ?
 
Okay... I'm drawing a blank.

I do a query... example:

Code:

$qry=$db->query_read("SELECT field1,field2,field3 from vb_xyz WHERE field4='myvalue' and field8='Not_Processed'");

// SET FIELD8 TO PENDING TO STOP OVERLAPPING CRONS

while ($result=$db->fetch_array($qry)) {

    $db->query_write("UPDATE vb_xyz SET field8='Pending' WHERE field1='$result[field1]'");

}

// BEGIN PERFORMING ACTIONS, UPDATE DB FOR EACH FIELD ONCE COMPLETE

while ($result2=$db->fetch_array($qry)) {

    // action code here
    // this is what is not working, "$qry" seems to be "empty" or "finished"

}

So, my hang up (and maybe someone can suggest a better way to do this) is with this second "while" loop. I want to keep overlapping cron runs (example, running every minute, but one cron launch script lasts longer than 1 minute) from duplicating efforts.

In the first loop, I update a given field to "Pending" status and in the second loop, I actually take action. I can't do this with one loop because I want to make sure that the Pending status is applied to the entire selected $qry before running a process, so no future spawned scripts duplicate what a previously spawned script is already attempting to complete.

I know in PHP you can take an array and set the counter back to 0. But, how do I do this with the mysql query and the while clause using fetch_array?

If you know of a better way to do what I want to do, Im open for suggestions.

Thanks!

PS - Above code is for example purposes only and not copied from my actual working code.

Paul M 08-26-2006 02:18 AM

In the first loop add a line to store each row in an array, then loop round that array for your second set of processing.

DigitalCrowd 08-26-2006 02:24 AM

Ah Ha! Why did I not think of that. Guess when your looking in one direction, sometimes other directions are blind.

I'd still be open to reseting that counter for the $qry if anyone knows how to do that. But, in the mean time... Paul's idea would solve my issue.

Code Monkey 08-26-2006 02:34 AM

Or, better yet, you can use mysql_data_seek();

PHP Code:

$qry=$db->query_read("SELECT field1,field2,field3 from vb_xyz WHERE field4='myvalue' and field8='Not_Processed'");

// SET FIELD8 TO PENDING TO STOP OVERLAPPING CRONS

while ($result=$db->fetch_array($qry)) {

    
$db->query_write("UPDATE vb_xyz SET field8='Pending' WHERE field1='$result[field1]'");

}



mysql_data_seek($qry0);




// BEGIN PERFORMING ACTIONS, UPDATE DB FOR EACH FIELD ONCE COMPLETE

while ($result2=$db->fetch_array($qry)) {

    
// action code here
    // this is what is not working, "$qry" seems to be "empty" or "finished"




DigitalCrowd 08-26-2006 02:42 AM

Thank you. That worked perfectly. I think I need a vacation, I should have known that answer. ;)

mihai11 10-18-2007 07:09 AM

Quote:

Originally Posted by Code Monkey (Post 1060723)
Or, better yet, you can use mysql_data_seek();

I also had trouble with this and your answer helped. Thank you.

mihai11 01-10-2008 03:22 PM

Looking thru the code, I found this function:

PHP Code:

function data_seek($queryresult$index

in the file class_code.php.

This seems to be doing what the OP requested using the VB coding standards.

Dismounted 01-13-2008 10:04 AM

Yes, you should use the $db->data_seek() alias for the function, as this keeps the database layer in there, which is needed when vBulletin includes different layers to cater for different database types.


All times are GMT. The time now is 02:13 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01120 seconds
  • Memory Usage 1,735KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (2)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (8)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete