以下是对我的问题的反馈:
我想发布我要分享的complet代码。此函数执行存储过程并收集此过程选择的所有数据和所有输出参数
function executeProc($sProcName, $sInParamTypes = "", $aInParam = null, $iNumberParameterOut=0)
{
global $oError;
$oError = false;
$aRows = array();
$sCallStr = "";
$sSelectOutParam = "";
$aReturn = array();
if($aInParam == null)
{
$aInParam = array();
}
$iLevel = error_reporting(9);
// open connection to DB
$oConnection = @mysqli_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DBNAME);
if ($oConnection === false)
{
$oError = mysqli_error($oConnection);
return false;
}
// we set the character set
mysqli_set_charset($oConnection, "utf8");
// we build the call to the stored procedure
$sCallStr = "CALL ";
$sCallStr = $sCallStr.$sProcName."(";
// we add the IN parameters
foreach($aInParam AS $sParam)
{
$sCallStr = $sCallStr."?,";
}
// we add the OUT parameters (since we can't access directly the out parameters, we store their value in variables @ on the DB) we will access theme at the end
for($iIndex=0; $iIndex
{
$sSelectOutParam = $sSelectOutParam."@outParam".$iIndex.",";
}
// we remove the unwanted coma
if($iNumberParameterOut > 0)
{
$sSelectOutParam = rtrim($sSelectOutParam, ",");
}
else
{
$sCallStr = rtrim($sCallStr, ",");
}
// we merge the two parameters string and add the final parenthesis
$sCallStr = $sCallStr.$sSelectOutParam.")";
// we prepare the call
$oMysqlCall = mysqli_prepare($oConnection, $sCallStr);
// only if we have parameters we bind the call with the types and the list of IN parameters. If no parameters is waited by the stored procedure we don't need to bind
if(count($aInParam) > 0)
{
mysqli_stmt_bind_param($oMysqlCall, $sInParamTypes, ...$aInParam);
}
// we execute the stored procedure
mysqli_stmt_execute($oMysqlCall);
// we get the data that would be returned by select in the procedure
while ($oResult = mysqli_stmt_get_result($oMysqlCall))
{
if (mysqli_num_rows($oResult) > 0)
{
while ($oRow = mysqli_fetch_assoc($oResult))
{
foreach($oRow as $key => $val)
{
$aRows[$key] = $val;
}
$aReturn[] = $aRows;
$aRows = array();
}
}
// we free the return variable
mysqli_free_result($oResult);
// we get the next return
mysqli_stmt_next_result($oMysqlCall);
}
// we have to close the call to get the out parameters
mysqli_stmt_close($oMysqlCall);
if($iNumberParameterOut > 0)
{
// to get the out parameters stored in variables we have to make a select on on the DB
$oResult = mysqli_query($oConnection, "SELECT ".$sSelectOutParam.";");
// for every variable we create a row and add to an array
for($iIndex=0; $iIndex
{
$iReturnIndex = 0;
while($oRow = mysqli_fetch_assoc($oResult))
{
if ($oRow===false) $oRow = array();
$aRows[key($oRow)] = $oRow[key($oRow)];
$iReturnIndex++;
}
if ($oRow===false && $iReturnIndex==0)
{
$aRows[0] = array();
}
}
// stored in the out key of our main return variable
$aReturn["out"] = $aRows;
}
// we free the rest
mysqli_free_result($oResult);
mysqli_close($oConnection);
error_reporting($iLevel);
// and return the data
return $aReturn;
}
}
希望这能有所帮助。随意评论或询问您是否理解某事