我正在尝试使用 CodeIgniter 3.x 将数据插入到 MySqli 表中。
出于某种原因,CodeIgniter 查询生成器中没有定义任何数据库方法 documentation似乎为我工作。
使用
$data = array(
'created_at' => date('Y/m/d h:i:s'),
'updated_at' => date('Y/m/d h:i:s'),
'first_name' => $firstName,
'last_name' => $lastName,
'email' => $email,
'auth_key' => $password,
'enabled' => '1',
'deleted' => '0');
$test = $this->db->insert('users', $data);
给我错误:调用未定义的方法“插入”。这是 CodeIgniter 文档中定义的方法。我从我的 Controller 调用此代码,尽管我也在我的模型中尝试过它。
我已经能够使用此处的代码成功运行 SQL 语句并从数据库获取数据:
$query = $this->db->table('users')
->select('*')
->where($this->table . '.auth_key', $password)
->where($this->table . '.email', $email)
->where($this->table . '.enabled', 1)
->get()->getRowArray();
但是,这与我在文档中看到的语法不同。如何将 $data 数组插入 MySQLi DB 中的 users 表中?
谢谢,如果我需要澄清任何事情,请告诉我!
最佳答案
使用 Insert_String 而不是 Insert 可能会解决您的问题,
$data = array(
'created_at' => date('Y/m/d h:i:s'),
'updated_at' => date('Y/m/d h:i:s'),
'first_name' => $firstName,
'last_name' => $lastName,
'email' => $email,
'auth_key' => $password,
'enabled' => '1',
'deleted' => '0');
$test = $this->db->insert_string('users', $data);
关于php - 将数据插入数据库 CodeIgniter 3.x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50322526/