dpwh11290 2011-09-11 20:34
浏览 36
已采纳

准备好的陈述 - 创建单一方法*

FYI. ended up going with PDO solution as this was simpler.

I'm trying to add a single method to handle all queries to the database. I want the queries to use parameter binding. How do I handle a variable amount of function parameters in mysqli_stmt_bind_param()?

This post here led me to understand the pros of parameter binding.

Here is my example code..where I am currently stuck at is marked.

INPUT PARAMETERS

  $query = "INSERT INTO b0 VALUES (?, ?, ?)"
  $par_arr = {'bookmark', 'http://www.bookmark.com', 'tag'}

PROTOTYPE CODE

  protected static function query($query, $par_arr)
    {
    if($statement=mysqli_prepare(one::$db, $query) 
      {
      mysqli_stmt_bind_param($statement, "s", ...variable amount of parameters...);<----how should this be handled?
      ...
  • 写回答

3条回答 默认 最新

  • duandu1377 2011-09-11 21:45
    关注

    Update 2: If you experience any further problems with this code, then you should probably follow this advice and use PDO instead.

    This is how you should be using call_user_func_array [docs]:

    protected static function query($query, $types, $values) {
        if($statement = mysqli_prepare(one::$db, $query) {
            $parameters = array_merge(array($statement, $types), $values);    
            call_user_func_array('mysqli_stmt_bind_param', $parameters);
            // ...
        }
    }
    

    where $types is a string indicating the type of each value, as described in the mysqli_stmt_bind_param documentation (call_user_func_array is even mentioned there).


    Update: It seems it is not that easy after all, and you have to create references to the values first:

     foreach($values as $k => $v) {
         $values[$k] = &$v;
     }
    
     $parameters = array_merge(array($statement, $types), $values);
     call_user_func_array('mysqli_stmt_bind_param', $parameters);
     // ...
    

    call_user_func_array is for user defined functions per php.net

    No it's not. The first parameter is of type callback, and the documentation says (emphasis mine):

    A PHP function is passed by its name as a string. Any built-in or user-defined function can be used, except language constructs such as: array(), echo(), empty(), eval(), exit(), isset(), list(), print() or unset().

    Next remark:

    is just used to simplify syntax for passing arrays to user defined functions

    Have you had a look at the examples? Each element of the array you pass to call_user_func_array will be passed as argument to the function you specify. Arrays are the only way to have a collection of values of variable size.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 爬虫爬取网站的一些信息
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错