dynamic prepared insert statement

前端 未结 2 507
孤独总比滥情好
孤独总比滥情好 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:35

    public function create() {
        $db = Database::getInstance();
        $mysqli = $db->getConnection();
    
        $attributes = $this->sanitized_attributes();
    
        $tableName = static::$table_name;
    
        $columnNames = array();
        $placeHolders = array();
        $values = array();
    
        foreach($attributes as $key=>$val)
        {
            // skip identity field
            if ($key == static::$identity)
                continue;
            $columnNames[] = '`' . $key. '`';
            $placeHolders[] = '?';
            $values[] = $val;
        }
    
        $sql = "Insert into `{$tableName}` (" . join(',', $columnNames) . ") VALUES (" . join(',', $placeHolders) . ")";
    
        $statement = $mysqli->stmt_init();
        if (!$statement->prepare($sql)) {
            die("Error message: " . $mysqli->error);
            return;
        }
    
        $bindString = array();
        $bindValues = array();
    
        // build bind mapping (ssdib) as an array
        foreach($values as $value) {
            $valueType = gettype($value);
    
            if ($valueType == 'string') {
                $bindString[] = 's';
            } else if ($valueType == 'integer') {
                $bindString[] = 'i';
            } else if ($valueType == 'double') {
                $bindString[] = 'd';
            } else {
                $bindString[] = 'b';
            }
    
            $bindValues[] = $value;
        }
    
        // prepend the bind mapping (ssdib) to the beginning of the array
        array_unshift($bindValues, join('', $bindString));
    
        // convert the array to an array of references
        $bindReferences = array();
        foreach($bindValues as $k => $v) {
            $bindReferences[$k] = &$bindValues[$k];
        }
    
        // call the bind_param function passing the array of referenced values
        call_user_func_array(array($statement, "bind_param"), $bindReferences);
    
        $statement->execute();  
        $statement->close();
    
        return true;
    }
    

    I want to make special note that I did not find the solution myself. I had a long time developer find this solution and wanted to post it for those that might want to know.

提交回复
热议问题