title: Random Broken Search Feature in Codeigniter, Gives Database Error?
body: <p>Here is the server version info I know you might need:</p>
  <ul>
  <li>CodeIgniter Version: 3.1.9</li>
  <li>PHP Version: 7.3.6</li>
  <li>MySQL Server Version: 5.7.27</li>
  </ul>
  <p>I am new to php and web design in general.</p>
  <p>This is particularly true when it comes to CodeIgniter's PHP shorthand tools. A friend of mine who has become increasingly hard to get in touch with due to her &quot;day job&quot; is the one who wrote the code for this site a couple years ago. Recently, I have been on my own with it for awhile now and I am learning a lot. But, I recently encountered a small issue that I wonder if anyone could help me with:</p>
  <p>Not too long ago my recipe search feature worked flawlessly. Until now. I rarely edit anything that builds or drives the search feature of my site. However, recently I have been getting the following error when I type anything into the search field:</p>
  <p>Error screenshot:</p>
  <p><a href="https://gyazo.com/d949731310229541b33ead40a806c333" rel="nofollow noreferrer">https://gyazo.com/d949731310229541b33ead40a806c333</a></p>
  <p>Recipe.php (Recipe Controller)</p>
  <pre><code>&lt;?php
  class Recipe extends MY_Controller{
  
   function __construct() {
    parent::__construct();
     $uri = $this-&gt;uri-&gt;uri_string();
     $this-&gt;session-&gt;set_flashdata('uri', $uri);
   }
      
   function index(){
    $recipe_linkname = $this-&gt;uri-&gt;rsegment(3);
    if(!$recipe_linkname){
     $recipe_linkname = $this-&gt;recipe_model-&gt;first_linkname();
    }
    $recipe = $this-&gt;recipe_model-&gt;read_record($recipe_linkname);
    if(count($recipe) == 0){
     show_404('');
     die();
    }
    $data = array(
       'title' =&gt; &quot;Shirley's Recipes:&quot;.$recipe['name'],
     'columns' =&gt; array('toc', 'recipe'),
      'recipe' =&gt; $recipe
    );
    $this-&gt;load-&gt;view('includes/template', $data);
   }
      
   function flipbook(){
    $this-&gt;require_login();
    $this-&gt;load-&gt;library('pagination');
      
          $config['base_url'] = site_url('recipe/flipbook');
        $config['total_rows'] = $this-&gt;recipe_model-&gt;num_rows();
       $config['uri_segment'] = '3';
          $config['per_page'] = 1;
         $config['num_links'] = 1;
     $config['display_pages'] = TRUE;
     $config['full_tag_open'] = '&lt;div id=&quot;pagination&quot;&gt;';
    $config['full_tag_close'] = '&lt;/div&gt;';
    
    $this-&gt;pagination-&gt;initialize($config);
    $recipe = $this-&gt;recipe_model-&gt;read_records($config['per_page'], $this-&gt;uri-&gt;segment(3))-&gt;row_array();
      $data = array(
       'title' =&gt; &quot;Shirley's Recipes:&quot;.$recipe['name'],
     'columns' =&gt; array('toc', 'recipe_flipbook'),
      'recipe' =&gt; $recipe
    );
    $this-&gt;load-&gt;view('includes/template', $data);
   }
    
   function search(){
     if($this-&gt;input-&gt;method() === 'post'){
      $search = $this-&gt;input-&gt;post('query');
     $results = $this-&gt;recipe_model-&gt;search($search);
        $data = array(
       'title' =&gt; &quot;Shirley's Recipes:Search Results&quot;,
     'columns' =&gt; array('toc', 'recipe_search'),
     'recipes' =&gt; $results,
     );
     $this-&gt;load-&gt;view('includes/template', $data);
     }
     else{
      redirect('recipe');
     }
   } 
      
   function add(){
    $this-&gt;require_login();
    if($this-&gt;input-&gt;post('submit')){
     $data = array(
            'name' =&gt; $this-&gt;input-&gt;post('name'),
      'buttontext' =&gt; $this-&gt;input-&gt;post('buttontext'),
        'linkname' =&gt; $this-&gt;input-&gt;post('linkname'),
             'img' =&gt; $this-&gt;input-&gt;post('img'),
            'time' =&gt; $this-&gt;input-&gt;post('time'),
        'category' =&gt; $this-&gt;input-&gt;post('category'),
     'subcategory' =&gt; $this-&gt;input-&gt;post('subcategory'),
    ' ingredients' =&gt; $this-&gt;input-&gt;post('ingredients'),
       'equipment' =&gt; $this-&gt;input-&gt;post('equipment'),
     'preparation' =&gt; $this-&gt;input-&gt;post('preparation'),
      'directions' =&gt; $this-&gt;input-&gt;post('directions')
     );
     $recipeid = $this-&gt;recipe_model-&gt;create_record($data);
     redirect('recipe/'.$this-&gt;input-&gt;post('linkname'));
    }
    else{
     $data = array(
           'title' =&gt; &quot;Shirley's Recipes:Add New Recipe&quot;,
         'columns' =&gt; array('toc', 'admin/recipe_edit'),
          'recipe' =&gt; array('name' =&gt; '',
      'buttontext' =&gt; '',
        'category' =&gt; '',
     'subcategory' =&gt; '',
        'linkname' =&gt; '',
             'img' =&gt; '',
            'time' =&gt; '',
     'ingredients' =&gt; '',
       'equipment' =&gt; '',
     'preparation' =&gt; '',
      'directions' =&gt; '')
     );
     $this-&gt;load-&gt;view('includes/template', $data);
    }
   }
  
   function edit(){
    $this-&gt;require_login();
    $id = $this-&gt;uri-&gt;segment(3);
    if($this-&gt;input-&gt;post('submit')){
     $data = array(
            'name' =&gt; $this-&gt;input-&gt;post('name'),
      'buttontext' =&gt; $this-&gt;input-&gt;post('buttontext'),
        'linkname' =&gt; $this-&gt;input-&gt;post('linkname'),
             'img' =&gt; $this-&gt;input-&gt;post('img'),
            'time' =&gt; $this-&gt;input-&gt;post('time'),
        'category' =&gt; $this-&gt;input-&gt;post('category'),
     'subcategory' =&gt; $this-&gt;input-&gt;post('subcategory'),
     'ingredients' =&gt; $this-&gt;input-&gt;post('ingredients'),
       'equipment' =&gt; $this-&gt;input-&gt;post('equipment'),
     'preparation' =&gt; $this-&gt;input-&gt;post('preparation'),
      'directions' =&gt; $this-&gt;input-&gt;post('directions')
      );
      $this-&gt;recipe_model-&gt;update_record($id, $data);
      redirect('recipe/'.$this-&gt;input-&gt;post('linkname'));
    }
    else{
     $recipe = $this-&gt;recipe_model-&gt;read_record($id);
     $data = array(
       'title' =&gt; &quot;Shirley's Recipes:Edit Recipe : &quot;.$recipe['name'],
         'columns' =&gt; array('toc', 'admin/recipe_edit'),
              'id' =&gt; $id,
      'recipe' =&gt; $recipe
     );
     $this-&gt;load-&gt;view('includes/template', $data);
    }
   }
      
   function delete(){
    $this-&gt;require_login();
    $this-&gt;recipe_model-&gt;delete_record($this-&gt;uri-&gt;segment(3));
    redirect('/');
   } 
  }
  // End - recipe.php
  ?&gt;
  </code></pre>
  <p>Recipe_model.php (Recipe Model)</p>
  <pre><code>&lt;?php
  class Recipe_model extends CI_Model {
  
   function create_record($data){
    if(isset($data['buttontext']) and $data['buttontext'] == ''){
     $data['buttontext'] = NULL;
    }
    $this-&gt;db-&gt;insert('recipe', $data);
    return $this-&gt;db-&gt;insert_id();
   }
    
   function read_records($limit, $offset){
    $this-&gt;db-&gt;order_by('name', 'asc');
    if(isset($limit) &amp;&amp; isset($offset)){
     $this-&gt;db-&gt;limit($limit, $offset);
    }
    $q = $this-&gt;db-&gt;get('recipe');
    return $q;
   }
    
   function read_record($key){
    if(is_numeric($key)){
     $this-&gt;db-&gt;where('id', $key);
    }
    else{
     $this-&gt;db-&gt;where('linkname', $key);
    }
    $q = $this-&gt;db-&gt;get('recipe');
    return $q-&gt;row_array();
   }
    
   function first_linkname(){
    $this-&gt;db-&gt;select('linkname, name');
    $this-&gt;db-&gt;order_by('name', 'asc');
    $this-&gt;db-&gt;limit(1);
    $q = $this-&gt;db-&gt;get('recipe');    
    return $q-&gt;row()-&gt;linkname;
   }
          
   function read_names(){
    $this-&gt;db-&gt;select('linkname, name');
    $this-&gt;db-&gt;order_by('name', 'asc');
    $q = $this-&gt;db-&gt;get('recipe');
    return $q-&gt;result();
   }
    
   function read_names_categories(){
    $this-&gt;db-&gt;select('buttontext, linkname, name, category, subcategory, img, time');
    $this-&gt;db-&gt;order_by('name', 'asc');
    $q = $this-&gt;db-&gt;get('recipe');
    return $q-&gt;result();  
   }
      
   function search($search){
    $terms = explode(&quot; &quot;, $search);
    $match = &quot;&quot;;
    foreach($terms as $term){
     $match .=  $term;
    }
    $querystr = &quot;SELECT *, MATCH(name, linkname, time, img, ingredients, equipment, preparation, directions, category, subcategory, keywords) AGAINST('&quot;.$match.&quot;') as score FROM recipe WHERE MATCH(name, linkname, time, img, ingredients, equipment, preparation, directions, category, subcategory, keywords) AGAINST('&quot;.$match.&quot;') ORDER BY score DESC;&quot;;
    $q = $this-&gt;db-&gt;query($querystr);
    return $q-&gt;result();
   }
  
   function update_record($id, $data){
    if(isset($data['buttontext']) and $data['buttontext'] == ''){
     $data['buttontext'] = NULL;
    }
    $this-&gt;db-&gt;where('id', $id);
    $this-&gt;db-&gt;update('recipe', $data);
   }
    
   function delete_record($id){
    $this-&gt;db-&gt;where('id', $id);
    $this-&gt;db-&gt;delete('recipe');
   }
    
   function num_rows(){
    return $this-&gt;db-&gt;get('recipe')-&gt;num_rows();
   }
  }
  // End - recipe_model.php
  ?&gt;
  </code></pre>
  <p>recipe_search.php (Search Output)</p>
  <pre><code>&lt;div id=&quot;recipelist&quot; class=&quot;content&quot;&gt;
   &lt;h1&gt;Recipe Search&lt;/h1&gt;
    &lt;div class=&quot;recipesearch&quot;&gt;
     &lt;ol&gt;
      &lt;?php if(!empty($recipes)): foreach($recipes as $recipe): ?&gt;
       &lt;li&gt;&lt;a href=&quot;/recipe/&lt;?=$recipe-&gt;id ?&gt;&quot;&gt;&lt;?=$recipe-&gt;name ?&gt;&lt;/a&gt;&lt;span class=&quot;confidence&quot;&gt;&lt;?=$recipe-&gt;score ?&gt;&lt;/span&gt;&lt;/li&gt;
       &lt;?php endforeach; ?&gt;
     &lt;/ol&gt;
    &lt;/div&gt;
    &lt;?php else: ?&gt;
    &lt;h3&gt;No matching recipes&lt;/h3&gt;
    &lt;?php endif; ?&gt;
  &lt;/div&gt;
  </code></pre>
  <p>Lastly, here is a screenshot of the database structure and field names.</p>
  <p>Database screenshot:</p>
  <p><a href="https://gyazo.com/b17307885e9ea4e7c619efba707a7e44" rel="nofollow noreferrer">https://gyazo.com/b17307885e9ea4e7c619efba707a7e44</a></p>
  <p>I hope that is everything you guys need to help me sort this out. I am also open to rewriting the PHP a different way if It will avoid breakage in the future. Any and all help you could offer is greatly appreciated.</p>
  <p>Thank you again!</p>