Dan
Dan

Reputation: 425

Codeigniter Active Record SQL syntax error

I have the following Active Record pattern within one of my models :

$this->db->get('names');
$this->db->like('name', $name);
$this->db->where('ratio >=', 0.75);
$this->db->order_by('ratio', 'desc');

$query = $this->db->get();

This gives me a syntax error :

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `ratio` >= 0.75 AND `name` LIKE '%JORDAN%' ORDER BY `ratio' at line 2

The full statement being returned is :

SELECT * WHERE `ratio` >= 0.75 AND `name` LIKE '%JORDAN%' ORDER BY `ratio` desc

I don't know why my table names isn't showing as I'm calling $this->db->get('names'); which should produce SELECT * FROM names, is it just not returning in the error? What's wrong with this statement and what should I do to correct my Active Record call?

Upvotes: 0

Views: 571

Answers (1)

Shomz
Shomz

Reputation: 37701

get needs to go at the end. If you want to select a table before - use $this->db->from('names'); and then just $this->db->get();. So the full code:

$this->db->like('name', $name);
$this->db->where('ratio >=', 0.75);
$this->db->order_by('ratio', 'desc');
$query = $this->db->get('names');

or the chaining version:

$query = $this->db->like('name', $name)
->where('ratio >=', 0.75)
->order_by('ratio', 'desc')
->get('names');

Using from:

$query = $this->db->from('names')
->like('name', $name)
->where('ratio >=', 0.75)
->order_by('ratio', 'desc')
->get();

Upvotes: 2

Related Questions