doskmc7870 2013-12-09 09:32
浏览 137
已采纳

使用mysqli_stmt_bind_param()绑定NULL值

I'm trying to convert a code I write to use the php function mysqli_stmt_bind_param() instead of replacing tag in prewritten statement which I believe is not a good pratice.
So here one of the function I have today:

$idTag1="###";
$requestReplaceArray = array("%PRODUCT_ID%","%PLATFORM_ID%","%STATUS_ID%","%DATE%","%COMMENT%",$idTag1);
$updateRequest="UPDATE REQUEST
                SET
                    id_product = %PRODUCT_ID%,
                    id_platform = %PLATFORM_ID%,
                    id_status = %STATUS_ID%,
                    last_modifier = '".$_SERVER['PHP_AUTH_USER']."',
                    last_modification_date='%DATE%',
                    last_comment = '%COMMENT%'
                WHERE id =".$idTag1;


function updateRequest($id, $productID, $platformID, $statusID, $date, $comment){
    global $requestReplaceArray, $updateRequest;
    escapeArguments($id, $productID, $platformID, $statusID, $date,$comment);
    /*if number value is empty replace by 'null'*/
    $productID=replaceEmptyValueByNull($productID);
    $platformID=replaceEmptyValueByNull($platformID);
    $statusID=replaceEmptyValueByNull($statusID);

    $dbConnection = getDbConnection();
    $replacement = array($productID, $platformID, $statusID,$date ,$comment, $id);
    $updateRequest = str_replace($requestReplaceArray, $replacement, $updateRequest);


    if(! $result = mysqli_query( $dbConnection, $updateRequest ) ) {
       mysqli_free_result($result);
        $dbConnection->close();
        return "Error MyU02, can't update the request #".$id;
    }else{
        mysqli_free_result($result);
        $dbConnection->close();
        return $id; 
    }
}

This code isn't perfect but it works except when a $date is NULL.

My objectives is to now use that synthax :

$requestReplaceString = "iiissi";
$updateRequest="UPDATE REQUEST
            SET
                id_product = ?,
                id_platform = ?,
                id_status = ?,
                last_modifier = '".$_SERVER['PHP_AUTH_USER']."',
                last_modification_date=?,
                last_comment = ?
            WHERE id =?";


function updateRequest($id, $productID, $platformID, $statusID, $date, $comment){
    global $requestReplaceString, $updateRequest;
    $dbConnection = getDbConnection();

    $stmt = mysqli_prepare( $dbConnection, $updateRequest);
    mysqli_stmt_bind_param($stmt, $requestReplaceString, $productID, $platformID, $statusID,$date ,$comment, $id);


    if(mysqli_stmt_execute($stmt) ) {
        mysqli_stmt_close($stmt);
        $dbConnection->close();
        return $id; 
    }else{
        mysqli_stmt_close($stmt);
        $dbConnection->close();
        return "Error MyU02, can't update the request #".$id;
    }
}

My main issue here is to set some value to null because trying to bind a php NULL is not working at all. So my question is how can I bind NULL with mysqli if it's possible ?

Edit: it does work and my mistake comes from my bad code, the code is now corrected.

  • 写回答

1条回答 默认 最新

  • dp152153 2013-12-09 09:41
    关注

    Just bind it. It works perfectly. Any null value will be sent to server as mysql's NULL.

    Also note that there is a fatal issue with your code: you are connecting to database for the every query. Which will just kill your server.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化