Advertisement
rfv123

Q23747835 - PHP -retrieve-variable-from-stored-proc

Jan 27th, 2017
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.95 KB | None | 0 0
  1. <?php
  2. include __DIR__ .'/__bootstrap__.php';
  3.  
  4. /* ---------------------------------------------------------------------------
  5.  * Procedure testing start
  6.  */
  7. $db = appDIC('getDbConnection', 'default'); // get the default db connection
  8. $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
  9. $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  10.  
  11. // setup the PHP variables that will be used
  12. $phpInParam     = 5;                  
  13. $phpInOutParam  = 404;          /* PHP InOut variable  ==> read and should be changed  */
  14. $phpOutParam    = null;         /* PHP Out   variable  ==> should be changed           */
  15.  
  16.  
  17. /* ----------------------------
  18.  * prepare the procedure...
  19.  */
  20. $sql = "call demoSpInOut(:phpInParam,
  21.                         @varInOutParam, /* mysql variable name will be read and update */
  22.                         @varOutParam)"; /* mysql variable name that will be written to    */
  23.  
  24. $stmt = $db->prepare($sql);
  25.  
  26.  
  27. /* ------------------------------------
  28.  * set all the parameters to the Query
  29.  * 1) bind the PHP variables
  30.  * 2) Set the SQL User INOUT variables
  31.  */
  32.  
  33. $stmt->bindParam(':phpInParam', $phpInParam, PDO::PARAM_INT);
  34.  
  35. $db->exec("SET @varInOutParam = $phpInOutParam"); // This is safe as it does not execute.
  36.  
  37. /*
  38.  * Execute the query
  39.  */
  40. $allOk = $stmt->execute();
  41.  
  42. /* ------------------------------------------------------------
  43.  * get all the output from the procedure into the PHP variables
  44.  */
  45. $sql = "SELECT @varInOutParam AS phpInOutParam,
  46.               @varOutParam   AS phpOutParam
  47.        FROM dual";
  48. $results = current($db->query($sql)->fetchAll());
  49.  
  50. $phpInOutParam = $results['phpInOutParam'];
  51. $phpOutParam   = $results['phpOutParam'];
  52.  
  53. /* --------------------------------------------------------------------
  54.  * show values of php variables after execution
  55.  */
  56. \Kint::dump($phpInParam, $phpInOutParam, $phpOutParam);
  57.  
  58. /* Output:
  59. ┌──────────────────────────────────────────────────────────────────────────────┐
  60. │                                 $phpInParam                                  │
  61. └──────────────────────────────────────────────────────────────────────────────┘
  62. string (1) "5"
  63. ┌──────────────────────────────────────────────────────────────────────────────┐
  64. │                                $phpInOutParam                                │
  65. └──────────────────────────────────────────────────────────────────────────────┘
  66. string (3) "409"
  67. ┌──────────────────────────────────────────────────────────────────────────────┐
  68. │                                 $phpOutParam                                 │
  69. └──────────────────────────────────────────────────────────────────────────────┘
  70. string (3) "-15"
  71. ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
  72. Called from +57 K:\developer\testmysql\pip2_startapp\shell\pdoCallProcedureSqlVars.php
  73. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement