Ryan
Ryan

Reputation: 14659

Code ignitier MySQL query

I'm getting an error with this function:

Controller

    $data['carSubwoofers'] = $this->db->get_where("department = 'MOBILE AUDIO' 
                             AND class = 'CAR STEREO' 
                             AND subclass 
                             IN ('SO CAR STEREO','SUBS','SO SUBS')")->result();


$this->load->view('Category/carSubwoofers',$data);

View

foreach($data->result() as $row) { ?>
 <?php echo $row->modelNumber; ?>
     <?php echo $row->name; ?>
 <?php } ?>

Code igniter shoots out this:

A Database Error Occurred

Error Number: 1064

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 ''MOBILE AUDIO' AND class = 'CAR STEREO' AND subclass IN ('SO CAR STEREO' at line 2

SELECT * FROM (department = 'MOBILE AUDIO' AND class = 'CAR STEREO' AND subclass IN ('SO CAR STEREO', 'SUBS', 'SO SUBS'))

Filename: C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\CodeIgniter_2.0.2\system\database\DB_driver.php

Line Number: 330

Upvotes: 1

Views: 160

Answers (3)

Josh
Josh

Reputation: 942

You can chain your conditionals together to make it look cleaner too.

$this->db->where('department', 'MOBILE AUDIO')->where('class', 'CAR STEREO')->where_in('subclass', array('SO CAR STEREO','SUBS','SO SUBS'))->get('tableName');

Upvotes: 4

mazlix
mazlix

Reputation: 6313

Your get_where() is totally off the documentation...

$query = $this->db->get_where('TABLE', array('id' => $id), $limit, $offset);

from the docs

so you would want something like $this->db->get_where("TABLE", array('MOBILE AUDIO' => 'CAR STEREO','class' =>'CAR STEREO'...)

Upvotes: 2

Mr Griever
Mr Griever

Reputation: 4023

Don't know codeigniter, but it looks like you've forgotten to assign $this->db a table to query.

Upvotes: 0

Related Questions