gpt4 book ai didi

php - PDO/Codeigniter 准备时出错

转载 作者:行者123 更新时间:2023-11-29 19:35:12 25 4
gpt4 key购买 nike

我正在尝试使用 PDO 和 codeigniter 将新行插入到我的数据库中。

这是我的功能:

function addStudent($st, $img){
$stat = $this->db->prepare('INSERT INTO students (sName, phone, email, image) VALUES (:sName, :phone, :email, :img)');
$stat->bindParam(':sName', $st['sName']);
$stat->bindParam(':phone', $st['phone']);
$stat->bindParam(':email', $st['email']);
$stat->bindParam(':img', $img);
$stat->execute();
return $this->db->insert_id();
}

由于某种原因,我收到此错误:

'Call to undefined method CI_DB_pdo_mysql_driver::prepare()' (on the first line of the function). (I'm not sure if it's a problem with prepare method specifically because i can use $this->db->query() method in my other functions).

我还将附加来自 codeigniterdatabase.php 文件的配置。

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
'dsn' => 'mysql:host=localhost;dbname=school;charset=utf8',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'school',
'dbdriver' => 'pdo',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

最佳答案

您应该使用事件记录而不是 PDO:

https://www.codeigniter.com/userguide3/database/query_builder.html

会像这样:

$stat = $this->db->prepare('INSERT INTO students (sName, phone, email, image) VALUES (:sName, :phone, :email, :img)');

致:

$this->db->insert('学生', array('column_name' => $variable,'column_name' => $variable,'column_name' => $variable,'column_name' => $变量,));

关于php - PDO/Codeigniter 准备时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41587915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com