除了一些消息来源说查询生成器语句已准备好,其他人说它们已准备好但未绑定,然后一些消息来源说它们已绑定等之外,我没有找到太多文档方面的内容。一个可靠的答案将不胜感激。
此外,如果我想将表单数据传递到存储在数据库中的数组中,则应如何修改以下代码?
$user_first = $this->input->post('user_first');
$data['user_first'] = $user_first;
//this above code works fine if I want to store each part of the form
//in the array individually
$data = array(
'user_first' => 'My title'
//How can I get 'user_first' to => $user_first?
);
$this->pdo->insert('users', $data);
谢谢。
几种方法
//adding name by name to an array
$data = array('user_first' => $this->input->post('user_first'));
adding the entire post array
//as u have the same "name" in the form than the array u are sending to the db insert method
$data = $this->input->post();
//in short $this->input->post() is $_POST array, but cleaned
//or getting the values from $_POST
$data = array('user_first' => $_POST['user_first']);
希望我的回答对你有帮助。
答案在很大程度上取决于"准备"的含义。"绑定"可以以一种非常类似于PDO的方式完成。但是,没有对应于PDOStatement::bindColumn
、PDOStatement::bindParam
或PDOStatement::bindValue
的方法。
与"绑定"PDO::prepare()
最直接的等价物如下
$sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?";
$this->db->query($sql, array(3, 'live', 'Rick'));
?
占位符将按照它们在数组中的显示顺序替换为数组中的值。输入值将被转义。query()
方法不支持将:name
的 PDO sytax 作为占位符。(有关查询绑定的 CI 文档。
通常,各种查询生成器方法组合在一起可以实现与PDO::prepare()
和PDOStatement::execute()
相同的整体效果。
PDOStatement
方法检索查询数据的功能(例如 execute()、fetch() 等)是通过调用 CI 数据库方法来"生成查询结果"来实现的。
假设我上面示例中的三个输入已由 a 发布,这是我如何完成将它们插入表中
的方法$data['id'] = $this->input->post('id');
$data['status'] = $this->input->post('status');
$data['author'] = $this->input->post('author');
$this->db-insert('some_table', $data);
如果元素名称与表列名称完全匹配,并且我们知道只有这些输入会被发布,则可以将上述内容简化为
$this->db-insert('some_table', $this->input->post());