C-Designer
C-Designer

Reputation: 89

change the way i query the database in codeigniter

I write Object-Oriented PHP, but recently I started learning CodeIgniter.

Problem is I have to use different syntax for each (when querying the database).

When writing OOP code I usually do this:

public function insertAdvert() {

    $query = $this->db->prepare("INSERT INTO rooms (title, description, postcode) VALUES (?, ?)");

    $query->bindValue(1, $this->title);
    $query->bindValue(2, $this->description);
    $query->execute();

}

And for CodeIgniter:

$sql = "INSERT INTO rooms (title, description) VALUES (?,?);"; 
$this->db->query($sql, array($this->title, $this->description));

Is there a way to query the database in CodeIgniter by using prepared statements? Both methods start to blend in and that's not good:/

Upvotes: 1

Views: 122

Answers (1)

Mike
Mike

Reputation: 24393

By default, the answer is "no". Codeigniter has its own query syntax and you can't bind values/parameters like you can with straight PDO.

However, there is a third-party modification that would allow you to use true PDO syntax in Codeigniter, which can be found here.

It involves replacing one of the core Codeigniter files, which is generally discouraged, but if you're up to it, give it a try.

Upvotes: 2

Related Questions