Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8363291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:08:33+00:00 2026-06-09T12:08:33+00:00

So I have this function to search entries in a MySQL database: <?php private

  • 0

So I have this function to search entries in a MySQL database:

<?php
private function SearchContributors($search) 
    {
    $search_pieces = explode(' ', $search);

    if (count($search_pieces) == 1 )
        {
        $this->db->like('firstname', $search);
        $this->db->or_like('lastname', $search);   
        $result = $this->db->get(); //the line from the error message below
        }   
    
    //Other stuff for 2 and more pieces
    
    return $result;
    }
?>

I use the function on two occasions.

Case A is a user initiated search and gets the search query from the URL (domain.com/contributors/?x=paul). This works fine.

<?php
if (isset($_GET['x']))
    {
    $x = $_GET['x']; 
    $result = $this->SearchContributors($x);
    }
?>

Case B is a backup for when a user enters an invalid slug name (domain.com/contributors/paul instead of domain.com/contributors/pauline-surname) and gets the search query directly:

<?php
$this->db->where('slug', $slug);
$result = $this->db->get();    
if ($result->num_rows() == 0)
    {
    $x = str_replace('-', ' ', $slug);
    $result = $this->SearchContributors($x);
    }
?> 

This returned an MySQL syntax error:

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 ‘WHERE firstname LIKE ‘%paul%’ OR lastname LIKE ‘%paul%” at line 2

SELECT * WHERE firstname LIKE ‘%paul%’ OR lastname LIKE ‘%paul%’

Filename: /www/htdocs/w00a94ee/c/controllers/contributors.php

Line Number: 23

The function gets the very same string paul in both cases, so why doesn’t it work?

//EDIT

function __construct()
    {
    parent::__construct();
    $this->load->database('databasename');
    $this->db->from('tablename');
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:08:35+00:00Added an answer on June 9, 2026 at 12:08 pm

    You forgot to specify which table you want to select FROM.

    $this->db->from('tablename');
    

    EDIT: The problem is you are adding the from in your constructor, then you are calling:

    $this->db->where('slug', $slug);
    $result = $this->db->get();  
    

    before calling SearchContributors. This runs the query and resets the variables.

    So, when you call SearchContributors, FROM is no longer set.

    You need to put $this->db->from('tablename'); inside SearchContributors and not the constructor. It’s usually a good idea to make model functions self-contained, and not require outside functions (such as __construct to call them).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code like this: function search_keyword(){ $keyword = trim($_POST['keyword']); $search_explode = explode( ,
I have code like this: function search_keyword(){ $keyword = trim($_POST['keyword']); $search_explode = explode( ,
I have a similar code snippet like this class Search { public function search($for,
i'm using autocomplete from jquery... and i have this: $('#name_search').autocomplete({ source: search/name.php, open: function(){
I have code like this: function search_keyword(){ $keyword = $this->db->escape_like_str(trim($_POST['keyword'])); $sql = ( SELECT
hai I have a magento web site. In this the search function compare with
Currently, I am doing a search function. Lets say in my database, I have
I want to have search results from SOLR ordered like this: All the documents
I have this mysql query using PHP and first part is working fine, but
I have a search script which logs every search query in a MySQL database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.