11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 32 • STORED ROUTINESwww.it-ebooks.info$db = new mysqli("localhost", "websiteuser", "jason", "corporate");// Assign the employeeID$eid = filter_var($_POST['employeeid'], FILTER_SANITIZE_NUMBER_INT);// Execute the stored procedure$stmt = $db->prepare("SELECT calculate_bonus(?) AS bonus");$stmt->bind_param('s', $eid);$stmt->execute();$stmt->bind_result($bonus);$stmt->fetch();?>printf("Your bonus is \$%01.2f",$bonus);Executing this example produces output similar to this:Your bonus is $295.02Retrieving Multiple RowsAlthough the above example should suffice for understanding how multiple rows are returned from astored routine, the following brief example makes it abundantly clear. Suppose you create a storedprocedure that retrieves information regarding company employees:CREATE PROCEDURE get_employees()SELECT employee_id, name, position FROM employees ORDER by name;This procedure can then be called from within a <strong>PHP</strong> script like so:// Execute the stored procedure$result = $db->query("CALL get_employees()");// Loop through the resultswhile (list($employee_id, $name, $position) = $result->fetch_row()) {echo "$employee_id, $name, $position ";}Executing this script produces output similar to the following:646

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

Saved successfully!

Ooh no, something went wrong!