dynamic variables binding in mysqli bind_param

前端 未结 2 1353
南方客
南方客 2021-01-14 08:04

when i try to below code it give me a warning

mysqli_stmt::bind_param(): Number of elements in type definition string doesn\'t match number of bind variables         


        
2条回答
  •  感动是毒
    2021-01-14 09:08

    This is a late answer, I hope will help someone.

    It's a part of my mysqliLayer class, so I extracted needed variables.

    // establish mysqli connection
    $conn = new mysqli(.....); 
    $tableName = 'users';
    // Types to bind
    $type = 'isss';
    $fields = ['id','name', 'email', 'created'];
    $values = [1, 'angel', 'email@test.com', '2018-1-12'];
    
    $sql = "INSERT INTO " . $tableName . " (" . join(',', $fields) . ") VALUES (?,?,?,?)";
    
    $stmt = $conn->prepare($sql);
    // Using ...token introduced in php v.5.6 instead of call_user_func_array
    // This way references can be omitted, like for each value in array
    $stmt->bind_param($type, ...$values);
    
    $stmt->execute();
    
    $stmt->close();
    

提交回复
热议问题