dynamic prepared insert statement

前端 未结 2 501
孤独总比滥情好
孤独总比滥情好 2021-01-17 01:18

Let me preface that I just started learning prepared statements so much of this might just be to much to grasp, but I want to try.

I am trying to make a dynamic crea

2条回答
  •  [愿得一人]
    2021-01-17 01:48

    I accidently found your old post as I was trying myself to find a solution to the exact same problem. My code seems a bit more advantagous as there is only one loop included. Therefore I will add it as a possible improvement to this post:

        $sqlquery = $this->MySQLiObj->prepare($dummy);
    
        $paramQuery = array();
        $paramQuery[0] = '';
    
        $n = count($valueArray);
        for($i = 0; $i < $n; $i++) {
            $checkedDataType = $this->returnDataType($valueArray[$i]);
            if($checkedkDataType==false) {
                return false;
            }
            $paramQuery[0] .= $checkedDataType;
            /* with call_user_func_array, array params must be passed by reference -> & */
        $paramQuery[] = &$valueArray[$i];
        }
    
        /*In array(): sqlquery(object)->bind_param(method)*/
        call_user_func_array(array($sqlquery, 'bind_param'), $paramQuery);
        $sqlquery->execute();
        /*Can be used identical to $result = $mysqli->query()*/
        $result = $this->MySQLiObj->get_result();
        $sqlquery->close();
    

    Utilizing the function returnDataType() with a switch statement, which might be faster if there is a preference for a certain data type.

        private function returnDataType($input) {
        switch(gettype($input)) {
            case string: return 's';
            case double: return 'd';
            case integer: return 'i';
            default: $this->LOG->doLog("Unknown datatype during database access."); return 's';
        }
    }
    

提交回复
热议问题