PDO/Codeigniter error with prepare

Asked at 2017-01-11 10:08:27Z
  • 5 Subscribers

I'm trying to insert a new row into my database using PDO and codeigniter.

Here's my function :

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);
        return $this->db->insert_id();

for some reason I am getting this error :

'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).

I'll also attach the configuration of my database.php file from codeigniter.

$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

1 answers in total

Callombert Posted at 2017-01-11 16:21:37Z

You should be using active record instead of PDO:


Would go something like this:

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


$this->db->insert('students', array('column_name' => $variable,'column_name' => $variable,'column_name' => $variable,'column_name' => $variable,));

Answer this questsion