PDO/Codeigniter error with prepare

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

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);
        $stat->execute();
        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

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

You should be using active record instead of PDO:

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

Would go something like this:

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

To:

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

Answer this questsion