title: Error Number: 1064 CodeIgniter query builder
body: <blockquote>
    <p>You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') AND <code>instrument</code>.<code>type</code> = 2 ORDER BY <code>instrument</code>.<code>price</code> DESC LIMIT 100' at line 6</p>
  </blockquote>
  
  <pre><code>SELECT 
      `instrument`.`id` as `id`, 
      `instrument`.`name` as `name`, 
      `price`, 
      `isix`, 
      `wkn`, 
      `isin`, 
      `instrument`.`account` as `accID`, 
      `own_invest`, 
      `interest`, 
      (select company 
          from chart_of_accounts 
          where chart_of_accounts.id = accID 
          group by company
      ) as company_id, 
      (select url 
          from company 
          where company.id = company_id
      ) as company_url, 
      `tiv_bond_type`.`name` as `bond_type` 
  FROM 
      `instrument` 
  LEFT JOIN `bond` ON `bond`.`instrument` = `instrument`.`id` 
  LEFT JOIN `tiv_bond_type` ON `tiv_bond_type`.`bond_type` = `bond`.`type` 
  WHERE 
      ( ) AND 
      `instrument`.`type` = 2 
  ORDER BY 
      `instrument`.`price` DESC LIMIT 100
  </code></pre>
  
  <p>Filename: models/Search_model.php</p>
  
  <p>Line Number: 251</p>
  
  <pre><code>public function search_bonds($name, $isin, $isix, $wkn, $type, $initial_price, $end_price, $initial_interest, $end_interest) {
          $bond_name = explode(" ", $name);
  
          $this-&gt;db-&gt;select("instrument.id as id, instrument.name as name, price, isix, wkn, isin, instrument.account as accID, own_invest, interest,
                              (select company from chart_of_accounts where chart_of_accounts.id = accID group by company) as company_id,
                              (select url from company where company.id = company_id) as company_url,
                              tiv_bond_type.name as bond_type");
          $this-&gt;db-&gt;from("instrument");
          $this-&gt;db-&gt;join("bond", "bond.instrument = instrument.id", "left", "outer");
          $this-&gt;db-&gt;join("tiv_bond_type", "tiv_bond_type.bond_type = bond.type", "left");
          if (strlen($isin) &gt; 3)
              $this-&gt;db-&gt;where("instrument.isin", $isin);
  
          if (strlen($name) &gt; 1) {
              $this-&gt;db-&gt;group_start();
              $this-&gt;create_search_permutations($bond_name, 1);
              $this-&gt;db-&gt;group_end();
          }
  
          if (strlen($isix))
              $this-&gt;db-&gt;where("instrument.isix", $isix);
  
          if (strlen($wkn) &gt; 3 )
              $this-&gt;db-&gt;where("instrument.wkn", $wkn);
  
          if (strlen($type) &amp;&amp; is_numeric($type))
              $this-&gt;db-&gt;where('bond.type', $type);
  
          if (is_numeric($initial_price) &amp;&amp; is_numeric($end_price)) {
              $this-&gt;db-&gt;group_start();
              $this-&gt;db-&gt;where("instrument.price &gt;= ", $initial_price);
              $this-&gt;db-&gt;where("instrument.price &lt;= ", $end_price);
              $this-&gt;db-&gt;group_end();
          }
          else if (is_numeric($initial_price) &amp;&amp; ! is_numeric($end_price))
              $this-&gt;db-&gt;where("instrument.price &gt;= ", $initial_price);
          else if (is_numeric($end_price)  &amp;&amp; ! is_numeric($initial_price))
              $this-&gt;db-&gt;where("instrument.price &lt;= ", $end_price);
  
          if (is_numeric($initial_interest) &amp;&amp; is_numeric($end_interest)) {
              $this-&gt;db-&gt;group_start();
              $this-&gt;db-&gt;where("bond.interest &gt;= ", $initial_interest);
              $this-&gt;db-&gt;where("bond.interest &lt;= ", $end_interest);
              $this-&gt;db-&gt;group_end();
          }
          else if (is_numeric($initial_interest) &amp;&amp; ! is_numeric($end_interest))
              $this-&gt;db-&gt;where("bond.interest &gt;= ", $initial_interest);
          else if (is_numeric($end_price)  &amp;&amp; ! is_numeric($initial_interest))
              $this-&gt;db-&gt;where("bond.interest &lt;= ", $end_interest);
          $this-&gt;db-&gt;where("instrument.type", 2);
          $this-&gt;db-&gt;order_by("instrument.price", "desc");
          $this-&gt;db-&gt;limit(100);
          return $this-&gt;db-&gt;get()-&gt;result_array();
      }
  
          private function create_search_permutations($search_array, $table_name, $permutations = array()) {
          /*
              $query_type variable is defining the type of table column the query string is targeting
              ie $query_type == 1 defines that the query string is for instrument.name and $query_type == 2 is for company.name
          */
          if (empty($search_array)) {
              if ($table_name == 'instrument')
                  $this-&gt;db-&gt;or_where("instrument.name like '%".join('%', $permutations)."%'");
              elseif ($table_name == 'company')
                  $this-&gt;db-&gt;or_where("company.name like '%".join('%', $permutations)."%'");
              else
                  return;
          }
          else {
              for ($iterator = count($search_array) - 1; $iterator &gt;= 0; --$iterator) {
                  $new_search_array = $search_array;
                  $new_permutations = $permutations;
                  list($key) = array_splice($new_search_array, $iterator, 1);
                  array_unshift($new_permutations, $key);
                  $this-&gt;create_search_permutations($new_search_array, $table_name, $new_permutations);
              }
          }
      }
  </code></pre>
  
  <p>Anyone to helping with figuring out what is wrong with my code here.</p>