The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 16, 2019, 01:58:48 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Looping logic  (Read 1669 times)
Nieeru
n00b
*
Offline Offline

Posts: 1


View Profile
« on: November 04, 2010, 05:46:13 AM »

Hey there! Quite new to these forums.

I'm working on a script that fetches data from an external XML and stores it into a database.

What I want to achieve is to have set variables outside the loop defining 10 instances of classId as true, then loop the returned MySQL results and set unpopulated classId's as false.

I know that I can use if(count($data['classId'] == 1) < 1) whthin a foreach loop. But I can't use this kind of logic within a while loop from MySQL. I want to avoid having to create seperate queries for every instance of classId if possible.

I can't really find a solution to the problem either, so, I'm hoping that some of you guys can point me in the right direction.

Cheers,
- Nieeru
Logged
Phaėton
Lifer
*****
Offline Offline

Posts: 555


⎝⏠⏝⏠⎠


View Profile
« Reply #1 on: November 04, 2010, 08:59:32 AM »

How about using a stored procedure with a cursor and a handler?

example:

Code:
PROCEDURE movePW()
BEGIN
   declare thisID, thisPass varchar(64);
   declare keepGoing tinyint;
   declare original cursor for select id, user_pass from user1;
   declare continue handler for not found set keepGoing = 0;
   set keepGoing = 1;
   open original;
   fetch original into thisID, thisPass;
   while keepGoing = 1 do
      update wp_users set user_pass=thisPass where id=thisID;
      fetch original into thisID, thisPass;
      end if;
      fetch items into thisID;
   end while;
END
Logged

When I was your age we used to walk to the TV to change the channel....  _̴ı̴̴̡̡̡ ̡͌l̡̡̡ ̡͌l̡*̡̡ ̴̡ı̴̴̡ ̡̡͡|̲̲̲͡͡͡ ̲▫̲͡ ̲̲̲͡͡π̲̲͡͡ ̲̲͡▫̲̲͡͡ ̲|̡̡̡ ̡ ̴̡ı̴̡̡
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #2 on: November 04, 2010, 02:19:35 PM »

Nice Phae - am I to assume that code was built and tested using that bitchin phpMyIDE package?

(I'll comment on the first post in a bit, walking into a meeting ATM)
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Phaėton
Lifer
*****
Offline Offline

Posts: 555


⎝⏠⏝⏠⎠


View Profile
« Reply #3 on: November 05, 2010, 10:40:09 AM »

If you dont mind it Keepgoing forever... LOL
Logged

When I was your age we used to walk to the TV to change the channel....  _̴ı̴̴̡̡̡ ̡͌l̡̡̡ ̡͌l̡*̡̡ ̴̡ı̴̴̡ ̡̡͡|̲̲̲͡͡͡ ̲▫̲͡ ̲̲̲͡͡π̲̲͡͡ ̲̲͡▫̲̲͡͡ ̲|̡̡̡ ̡ ̴̡ı̴̡̡
Phaėton
Lifer
*****
Offline Offline

Posts: 555


⎝⏠⏝⏠⎠


View Profile
« Reply #4 on: November 05, 2010, 10:45:22 AM »

Yes, i have used phpMyide its awesome.... that code was actually not tested ... its rife with a few major issues but demonstrates the power of stored procedures, cursors and handlers to 'iterate' and 'test' completely on the Mysql server... good stuff!

theres a 'end if' where ther is no 'if' and it will keepgoing forever , assuming it doesnt error on the 'end if' with no 'if' first  Cheesy
Logged

When I was your age we used to walk to the TV to change the channel....  _̴ı̴̴̡̡̡ ̡͌l̡̡̡ ̡͌l̡*̡̡ ̴̡ı̴̴̡ ̡̡͡|̲̲̲͡͡͡ ̲▫̲͡ ̲̲̲͡͡π̲̲͡͡ ̲̲͡▫̲̲͡͡ ̲|̡̡̡ ̡ ̴̡ı̴̡̡
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!