-
68thorby68 Member
  
-
Posts: 133
Threads: 56
Joined: Apr 2020
Reputation:
1
06-09-2020, 04:46 AM
(This post was last modified: 06-10-2020, 02:40 AM by 68thorby68.)
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 !!!!!
-
israes Newbie

-
Posts: 1
Threads: 0
Joined: Apr 2020
Reputation:
0
01-28-2022, 12:37 AM
(This post was last modified: 01-28-2022, 12:39 AM by israes.)
I'm dealing with it now, and thanks to your post my code now works!!!
In addition, I found a quick fix related to this on php docs, take a look at example #5 https://www.php.net/manual/en/pdostateme...ample-1045
|