• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pass Array to Prepared Statements

#1
Hi ,
I'm tearing my hair out trying to fathom Prepared statements in Ci4 as per the documentation.

Could someone please help me understand why the follwing statement fails with error "mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement" undefined, when an array is passed to the execute statement, as per the documentation. I have tried enclosing the SQL ? parameter with parentheses (?) but get the same error?????

This throws the error

PHP Code:
use CodeIgniter\Model;
use 
CodeIgniter\Database\Query;

class 
Register extends Model {
    
    public function 
registerUser ($data) {
        
$db db_connect();
        
        
$dataArray=array(
            
$data['salutation'],
            
$data['first_name'],
            
$data['last_name'],
            
$data['telephone'],
            
$data['email'],
            
$data['password_hash'],
            
$data['question'],
            
$data['answer_hash'],
            
$data['status'],
            
$data['level'],
            
$data['confirm'],
            
$data['ip']
            );
        
        
$pQuery $db->prepare(function($db) {
            
$sql "INSERT INTO user_register (salutation, first_name, last_name, telephone, email, password, question, answer, status, level, confirm, ip) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        
            return (new 
Query($db))->setQuery($sql);
        }, 
$dataArray);
        
        try {
            
$pQuery->execute($dataArray);
        }catch (\
Exception $e) {
            return (
$e->getMessage());
        }
    }

the following doesn't
PHP Code:
use CodeIgniter\Model;
use 
CodeIgniter\Database\Query;

class 
Register extends Model {
    
    public function 
registerUser ($data) {
        
$db db_connect();
        
        
$dataArray=array(
            
$data['salutation'],
            
$data['first_name'],
            
$data['last_name'],
            
$data['telephone'],
            
$data['email'],
            
$data['password_hash'],
            
$data['question'],
            
$data['answer_hash'],
            
$data['status'],
            
$data['level'],
            
$data['confirm'],
            
$data['ip']
            );
        
        
$pQuery $db->prepare(function($db) {
            
$sql "INSERT INTO user_register (salutation, first_name, last_name, telephone, email, password, question, answer, status, level, confirm, ip) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        
            return (new 
Query($db))->setQuery($sql);
        }, 
$dataArray);
        
        try {
            
$pQuery->execute($data['salutation'],
            
$data['first_name'],
            
$data['last_name'],
            
$data['telephone'],
            
$data['email'],
            
$data['password_hash'],
            
$data['question'],
            
$data['answer_hash'],
            
$data['status'],
            
$data['level'],
            
$data['confirm'],
            
$data['ip']);
        }catch (\
Exception $e) {
            return (
$e->getMessage());
        }
    }


I just dont understand????


PLEASE HELP !!!!!
Reply

#2
I DO NOT believe Ci4 execute accepts arrays of data, but instead insists on each data element being defined indiviually. That said I believe a single data field can be an array.


Unfortunately this thread is a result of my mis-interpretation of the documentation.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.