'PDO equivalent to MDB2 autoExecute()
Alright, so normally Google and Stackoverflow are on my side, and I have good luck finding answers, but I could really use some help rewriting this MDB2 query execution for PDO. Most functions for MDB2 have a one-to-one equivalent in PDO, or something really close. However, according to this documentation, it auto prepares a query, and executes it. Here's this old code below:
$this->db->autoExecute('material_sets',
array('material_set' => filter_var($_REQUEST['material_set'], FILTER_SANITIZE_STRING),
'selectmethod' => $_REQUEST['selectmethod'],
'selectoptions' => $selectoptions,
'companyid' => $this->companyid),
MDB2_AUTOQUERY_INSERT);
Is there any modern PDO equivalent to this? If there isn't, I could really really use some help rewriting this, or at least a nudge in the right direction.
$query = 'INSERT INTO `material_sets` (material_set, selectmethod, selectoptions, companyid) VALUES (?,?,?,?)';
$this->db->prepare($query)->execute(array($_REQUEST['material_set'],$_REQUEST['selectmethod'],$selectoptions,$this->companyid));
I believe this snippet I wrote above may be an equivalent, but could use a sanity check here.
Edit: So I went to test this out, and it's throwing this error PHP Fatal error: Uncaught Error: Call to undefined method PDO::execute()
Which is just silly. I even tried to separate everything for sanity's sake, and it isn't liking that either.
$data = ['material_set' => filter_var($_REQUEST['material_set'], FILTER_SANITIZE_STRING),
'selectmethod' => $_REQUEST['selectmethod'],
'selectoptions' => $selectoptions,
'companyid' => $this->companyid];
$query = 'INSERT INTO `material_sets` (material_set, selectmethod, selectoptions, companyid) VALUES (?,?,?,?)';
$stmt = $this->db->prepare($query);
$stmt->execute($data);
I feel like it's dying at execute() for a reason that PHP can't properly output an error for.
I'm assuming $data is being formatted improperly, and dying? I need to investigate further.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
