11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

www.it-ebooks.infoCHAPTER 32 • STORED ROUTINESWHILEThe WHILE statement is common among many, if not all, modern programming languages, iterating one orseveral statements for as long as a particular condition or set of conditions remains true. Its prototype follows:[begin_label:] WHILE condition DOstatement_listEND WHILE [end_label]The test_data procedure first created in the above introduction to REPEAT has been rewritten, thistime using a WHILE loop:DELIMITER //CREATE PROCEDURE test_data(IN rows INT)BEGINEND//DECLARE val1 FLOAT;DECLARE val2 FLOAT;WHILE rows > 0 DOSELECT RAND() INTO val1;SELECT RAND() INTO val2;INSERT INTO analysis VALUES(NULL, val1, val2);SET rows = rows - 1;END WHILE;DELIMITER ;Executing this procedure produces similar results to those shown in the REPEAT section.Calling a Routine from Within Another RoutineIt’s possible to call a routine from within another routine, saving you the inconvenience of having torepeat logic unnecessarily. An example follows:DELIMITER //CREATE PROCEDURE process_logs()BEGINSELECT "Processing Logs";END//CREATE PROCEDURE process_users()BEGINSELECT "Processing Users";END//CREATE PROCEDURE maintenance()BEGINCALL process_logs();641

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!