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

前端 未结 5 2032
囚心锁ツ
囚心锁ツ 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 17:02

    call_user_func_array(array(&$stmt, 'bindparams'), $array_of_params);
    

    Didn't work for me in my environment but this answer set me on the right track. What actually worked was:

    $sitesql = '';
    $array_of_params = array();
    foreach($_POST['multiselect'] as $value){
        if($sitesql!=''){
            $sitesql .= "OR siteID=? ";
            $array_of_params[0] .= 'i';
            $array_of_params[] = $value;
        }else{
            $sitesql = " siteID=? ";
            $array_of_params[0] .= 'i';
            $array_of_params[] = $value;
        }
    }
    
    $stmt = $linki->prepare("SELECT IFNULL(SUM(hours),0) FROM table WHERE ".$sitesql." AND week!='0000-00-00'");
    call_user_func_array(array(&$stmt, 'bind_param'), $array_of_params);
    $stmt->execute();
    

提交回复
热议问题