php7.0mysql函数_用php7.0编程一个动态调用mysql存储过程的函数

以下是对我的问题的反馈:

我想发布我要分享的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;

}

}

希望这能有所帮助。随意评论或询问您是否理解某事

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值