private function SqlExecute($sql, $array = [], $param = []) { if(sizeof($array) == sizeof($param)) { $ini__li = $this->Connect(); $Query = $ini__li->prepare($sql); $pr = [ 0 => PDO::PARAM_INT, 1 => PDO::PARAM_STR ]; $i = 0; foreach($array as $key => $str) { $p = $param[$i]; $Query->bindParam($key, $str, $pr[$p]); $i++; } return $Query->execute(); }else{ return false; } } 

In the $param array, the values ​​0 or 1 are passed.
In the $array , the mask key and value.
Some queries are executed, and some are not. I can not find an error.

    1 answer 1

    PDOStatement :: bindParam Binds a PHP variable with a named or unnamed parameter of a prepared SQL query. Unlike PDOStatement :: bindValue (), a variable is bound by a link and its value will be calculated during a call to PDOStatement :: execute ().

    You pass the $ str variable, but it is clear that it is passed by reference and when you change the $ str variable it changes inside the request. During the cycle, you change it, that is, the cycle does it automatically.

    For this reason, I replaced the temporary loop variable $ str with the variable $ array [$ key], this variable will not change its value and during the query (execute) will have the same value as in the loop

     private function SqlExecute($sql, $array = [], $param = []) { if(sizeof($array) == sizeof($param)) { $ini__li = $this->Connect(); $Query = $ini__li->prepare($sql); $pr = [ 0 => PDO::PARAM_INT, 1 => PDO::PARAM_STR ]; $i = 0; foreach($array as $key => $str) { $p = $param[$i]; $Query->bindParam($key, $array[$key], $pr[$p]); $i++; } return $Query->execute(); }else{ return false; } } 
    • Heck. Works! Why? I do not see the difference. - Dmitriy
    • corrected the answer, now the moment will be visible where the difference is - Mykola Veriga
    • you can simply use PDOStatement :: bindValue () - Mykola Veriga
    • I noticed the difference in the code, I could not understand why my option does not work, and yours works. Well, you have already explained above. Thank. - Dmitriy