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 8682829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:52:41+00:00 2026-06-12T21:52:41+00:00

I need a little help with my code here. So far, when I go

  • 0

I need a little help with my code here. So far, when I go to the page, whole view is seen. When I set search and POST it, it works too. But when I add –> 2 or more <– keywords, i get an error. So the problem is in the forearch loop else construction.

In that case I get this error:

query fout 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 ‘ORDER BY custsurname, custforename, custmidname’ at line 6

I tried many ways of constructing, but somehow doesnt seem to work.

My code is below:

if(isset($_POST['search']))
{
    $search = $_POST['search'];
    $terms = explode(" ", $search);
    $customerlistquery = "
    SELECT * 
    FROM customer
    LEFT JOIN company
    ON customer.compid=company.compid
    WHERE 
    ";

    foreach ($terms as $each) 
    {
        $i++;

        if ($i == 1)
        {
            $customerlistquery .= "concat(custsurname, custforename, custmidname) LIKE '%$each%' ORDER BY custsurname, custforename, custmidname";
        }
        else
        {
            $customerlistquery .= "OR concat(custsurname, custforename, custmidname) LIKE '%$each%' ORDER BY custsurname, custforename, custmidname";
        }
    }
}
else
{
$customerlistquery = "  
        SELECT *
        FROM customer
        LEFT JOIN company
        ON customer.compid=company.compid
        ORDER BY custsurname, custforename, custmidname
        ";
}

And just one last more question. Why is PHP whining about undefined variable: i?

Notice: Undefined variable: i in ...

Is this a standard message at every “throwaway variable”?

  • 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-12T21:52:42+00:00Added an answer on June 12, 2026 at 9:52 pm

    @newfurniturey is right, the first time you use $i, is here: $i++; Basically, your adding 1 to something that doesn’t exist: $i hasn’t been declared, so there’s no value to which you can add 1.

    Yes, you could just turn the notices off, just hiding them. Your logs, however will get cluttered very quickly. It’s therefore good practice to write your code in such a way so that it runs under E_STRICT | E_ALL settings without any warnings or notices.

    After you’ve addressed this issue, I can tell you now that you’ll see SQL syntax errors all over the place, too:

    foreach ($terms as $i => $each) 
    {//use the index in a foreach loop, that's the easiest solution
        if ($i === 0)
        {
            $customerlistquery .= "concat(custsurname, custforename, custmidname) LIKE '%$each%' ORDER BY custsurname, custforename, custmidname";
        }
        else
        {
            $customerlistquery .= "OR concat(custsurname, custforename, custmidname) LIKE '%$each%' ORDER BY custsurname, custforename, custmidname";
        }
    }
    

    Each bit of query you concatenate ends in an ORDER BY clause. SQL won’t accept query’s that look like

    SELECT foo FROM db.bar WHERE x LIKE '%Y%' ORDER BY x,z OR x LIKE '%x%' ORDER BY x,z
    

    So I’d suggest you concat the ORDER BY clause after the loop:

    foreach ($terms as $i => $each) 
    {//use the index in a foreach loop, that's the easiest solution
        if ($i === 0)
        {
            $customerlistquery .= "concat(custsurname, custforename, custmidname) LIKE '%$each%'";
        }
        else
        {
            $customerlistquery .= "OR concat(custsurname, custforename, custmidname) LIKE '%$each%'";
        }
    }
    $customerlistquery .= ' ORDER BY custsurname, custforename, custmidname';
    

    Once you get this query to run, don’t be surprised if its dead slow: The top 3 things to slow query’s right down are:

    1. Bad indexing
    2. overuse of LIKE with wildcards
    3. Lots of OR clauses

    If your query has 2 of these three characteristics, there’s a 99% chance that SQL will perform a full table scan and write a temp table to disk. Far from ideal, then.

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

Sidebar

Related Questions

I need a little help here. This code correctly displays every format I enter--except
I would need little help here. I'm trying to get the git respository from
Need a little help with my jquery here I want all my button with
I need a little help setting up a HTTP Post in C#. I appreciate
I need a little help here please.What am trying to do is pull all
I need a little help creating a catch-all error handling page in my ICEfaces
I need a little help converting some VB.NET code to C#. I have tried
I need a little help. My code is work 99% correctly except for one
I need a little help here getting data from mysql via PHP with AS3.
I need a little help here.So basically I have to make a test of

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.