PHP + MYSQLI: Variable parameter/result binding with prepared statements

前端 未结 5 2148
囚心锁ツ
囚心锁ツ 2020-12-13 16:38

In a project that I\'m about to wrap up, I\'ve written and implemented an object-relational mapping solution for PHP. Before the doubters and dreamers cry out \"how on earth

5条回答
  •  甜味超标
    2020-12-13 16:54

    The more modern way to bind parameters dynamically is via the splat/spread operator (...).

    Assuming:

    • you have a non-empty array of values to bind to your query and
    • your array values are suitably processed as string type values in the context of the query and
    • your input array is called $values

    Code for PHP5.6 and higher:

    $stmt->bind_param(str_repeat('s', count($values)), ...$values); 
    

提交回复
热议问题