note 120227 added to mysqli.quickstart.stored-procedures

From: Date: Sun, 27 Nov 2016 21:39:46 +0000
Subject: note 120227 added to mysqli.quickstart.stored-procedures
Groups: php.notes 
Request: Send a blank email to php-notes+get-207702@lists.php.net to get a copy of this message
<?php /** * Small function to facilitate call procedure with multiple arguments (supports in/inout/out) */ $db = new mysqli('localhost', 'root', 'password', 'database'); $lt_query = callProcedure( $db, "stored_procedure", array( "in_param1" => "Value1", "in_param2" => "Value2", "inout_param3" => "Value3", "out_param4" => "", "out_param5" => "" )); function callProcedure( $po_db, $pv_proc, $pt_args ) { if (empty($pv_proc) || empty($pt_args)) { return false; } $lv_call = "CALL $pv_proc("; $lv_select = "SELECT"; $lv_log = ""; foreach($pt_args as $lv_key=>$lv_value) { $lv_query = "SET @_$lv_key = '$lv_value'"; $lv_log .= $lv_query.";\n"; if (!$lv_result = $po_db->query($lv_query)) { /* Write log */ return false; } $lv_call .= " @_$lv_key,"; $lv_select .= " @_$lv_key AS $lv_key,"; } $lv_call = substr($lv_call, 0, -1).")"; $lv_select = substr($lv_select, 0, -1); $lv_log .= $lv_call; if ($lv_result = $po_db->query($lv_call)) { if($lo_result = $po_db->query($lv_select)) { $lt_result = $lo_result->fetch_assoc(); $lo_result->free(); return $lt_result; } /* Write log */ return false; } /* Write log */ return false; } /** * This will return an array like this: * * $lt_query = array( * 'in_param1' = 'Value1', // Same value as in call * 'in_param2' = 'Value2', // Same value as in call * 'inout_param3' = ?, // Value is changed accordingly * 'out_param4' = ?, // Value is changed accordingly * 'out_param5' = ? // Value is changed accordingly * ) */ ?> ---- Server IP: 72.52.91.14 Probable Submitter: 90.149.137.15 ---- Manual Page -- http://php.net/manual/en/mysqli.quickstart.stored-procedures.php Edit -- https://master.php.net/note/edit/120227 Del: integrated -- https://master.php.net/note/delete/120227/integrated Del: useless -- https://master.php.net/note/delete/120227/useless Del: bad code -- https://master.php.net/note/delete/120227/bad+code Del: spam -- https://master.php.net/note/delete/120227/spam Del: non-english -- https://master.php.net/note/delete/120227/non-english Del: in docs -- https://master.php.net/note/delete/120227/in+docs Del: other reasons-- https://master.php.net/note/delete/120227 Reject -- https://master.php.net/note/reject/120227 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#207702) next »