PHP bind_param与数组

2 php mysqli prepared-statement bindparam

是否可以使用bind_param()数组?

例如

$stmt->bind_param('iss', Array(101, 'SomeString 1', 'Some string 2'));
// OR
$stmt->bind_param(Array('iss'), Array(101, 'String1', 'String2'));
// OR
$stmt->bind_param(Array( Array('i', 101), Array('s', 'String1'), Array('s', 'String2') ));
Run Code Online (Sandbox Code Playgroud)

这些示例中的任何一个都可以在PHP(或任何其他示例)中使用吗?

最后我可以使用我的函数/类

$sql->upload( $table, Array('n;id', 's;username=' . $username, 's;password=' . $password) );
Run Code Online (Sandbox Code Playgroud)

功能探索

public function upload( String $table , Array $values )

// Where example array could be Array('s;column=someString', 'i;SomeIntegerColumn=10', 'n;SomeID')
/*
    identifier;column=value
    Basically where identifier can be "n", "d", "i" or "s"
    column is the name of the sql column to inset the value to
    value is the string/integer/double to instert in the the column

    Example query with the array mentioned above would be
    "INSERT INTO $table(column, SomeIntegerColumn, SomeID) VALUES(?, ?, NULL)"
*/
Run Code Online (Sandbox Code Playgroud)

我的项目很快就会出现在GitHub上:)非常感谢!

You*_*nse 9

如果您的PHP没有过时(即=> 5.6),只需在第一个示例中添加三个点,

$stmt->bind_param('iss', ...array(101, 'SomeString 1', 'Some string 2'));
Run Code Online (Sandbox Code Playgroud)

  • @ringø难怪因为这个语法与bind_param无关,因为它是一个全语言特性,参数解包运算符 (2认同)