2 Comments


  1. Hello, I am a newb with php and working with your code to dynamically create the parameters

    I have tried this function this is copied & pasted from my project the code i have is this

    function set_params($stmt,$params){
        if ($params != null){
    
            $types = '';
            foreach ($params as $param)
            {
                if (is_int($param)) {
                    $types .= 'i';
                } elseif (is_float($param)) {
                    $types .='d';
                } elseif (is_string($param)){
                    $types .='s';
                } else {
                    $types .='b';
                }
            }
            
            $bind_names[] = $types;
            
            for ($i =0; $i < count($params); $i++){
                $bind_name = 'bind' . $i;
                $$bind_name = $params[$i];
                $bind_names[] = &$$bind_name;
            }
    
            call_user_func(array($stmt,'bind_param'),$bind_names);
        }
    
        return $stmt;
    
    and I have the parameter setup like you do in the example the only difference is that i have my query 
    set up in a function like this
    function db_query($sql, $params){
    
        //connect to the database
        $connection = db_connect();
    
        //query the database
        $stmt = $connection->prepare($sql);
        set_params($stmt,$params);
        $stmt->execute();
        $stmt->bind_result($result);
        $stmt->fetch();
        $stmt->close();
        return $result;
    }
    
    when i run the application and fill in my fields then hit my submit button i get the following errors
    Warning: Wrong parameter count for mysqli_stmt::bind_param() in E:\xampp\htdocs\dprojects\guildapplication\includes\functions.php on line 85
    
    line 85 in my code is
    call_user_func(array($stmt,'bind_param'),$bind_names);
    
    Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in E:\xampp\htdocs\dprojects\guildapplication\includes\functions.php on line 54
    line 54 in my code is inside my db_query function
    $stmt->bind_result($result);
    
    Warning: Wrong parameter count for mysqli_stmt::bind_param() in E:\xampp\htdocs\dprojects\guildapplication\includes\functions.php on line 85
    
     mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in E:\xampp\htdocs\dprojects\guildapplication\includes\functions.php on line 54
    
    Warning: Wrong parameter count for mysqli_stmt::bind_param() in E:\xampp\htdocs\dprojects\guildapplication\includes\functions.php on line 85
    
    these are the errors that i am getting with the code, I was wondering if you could help me pinpoint on 
    what is going wrong, either in your function or my db_query function.
  2. Daniel

    This was perfect. I can’t tell you how much you helped with the script.

    Thank you!!!

    If I may add one tiny detail:

    $stmt = DynamicBindVariables($stmt, $paramList);

    Now $stmt is a ‘mysqli_result Object’

     

     

Leave a Reply

Your email address will not be published.

*