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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:03:50+00:00 2026-05-16T15:03:50+00:00

I am building a search function on my website and I am having some

  • 0

I am building a search function on my website and I am having some trouble locating an error in the SQL. There are four SQL statements used to return the counts of the same search using the AND and the OR keywords. The second set of SQL statements return the actual results of the AND and the OR searches limited according to the page number of the current search.

Below I will show the SQL function for the and count and and search.
The code for the other two SQL statements is the exact same, only using OR instead of AND.

function _and_count($words) {

    require_once("include/conn.inc.php");

    $arraysize = count($words);

    $and_count_sql = "SELECT COUNT(DISTINCT tg_id)
                       FROM tg_keywords
                       WHERE tg_keyword=";

    if ($arraysize > 1) {
        foreach($words as $word){
            $count = 1;
            if ($count == 1){
                $and_count_sql .= "'".$word."' AND ";
            }
            elseif ($count < $arraysize){
                $and_count_sql .= "tg_keyword='".$word."' AND ";
            }
            else {
                $and_count_sql .= "'".$word."'";
            }
            $count++;
        }

    } elseif ($words == 1) {
        $and_count_sql .= "'".$words."'";
    }

    $and_count_result = mysql_query($and_count_sql)
        or die(mysql_error());

    return $and_count_result;

}


function _search_and($words, $startpos, $endpos) {

    require_once("include/conn.inc.php");

    $arraysize = count($words);

    $search_and_sql = "SELECT DISTINCT tg_id COUNT(*) AS nb
                       FROM tg_keywords
                       LEFT JOIN tg_info
                       ON tg_info.tg_id=tg_keywords.tg_id
                       WHERE tg_keyword=";

    if ($arraysize > 1){
        foreach($words as $word) {
            $count = 1;
            if ($count == 1 && $count < $arraysize) {
                $search_and_sql .= "'".$word."' AND ";
            } elseif ($count == 1 && $count == $arraysize) {
                $search_and_sql .= "'".$word."'";
            } elseif ($count < $arraysize) {
                $search_and_sql .= "tg_keyword='".$word."' AND ";
            } elseif ($arraysize == $count) {
                $search_and_sql .= "'".$word."'";
            }
            $count++;
        }
    } elseif ($arraysize == 1) {
        $search_and_sql .= "'".$words."'";
    } else {
        $search_and_sql .= "''";
    }

    $search_and_sql .= "GROUP BY tg_id
                        ORDER BY nb DESC, tg_info.date_added ASC
                        LIMIT ".$endpos." OFFSET ".$startpos;

    $search_and_result = mysql_query($search_and_sql, $link)
        or die(mysql_error());

    $and_array = mysql_fetch_array($search_and_result);

    return $and_array;


}

Any advice would be greatly appreciated.
Thanks
Archie

  • 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-05-16T15:03:50+00:00Added an answer on May 16, 2026 at 3:03 pm
     $search_and_sql = "SELECT DISTINCT tg_id COUNT(*) AS nb

    missing comma between tg_id and Count.

    Its good practise to show generated query, ie. echo $search_and_sql; before mysql_query

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

Sidebar

Related Questions

There are several plugin options for building a search engine into your Ruby on
Stack Overflowers: I have a search function on my company's website (based on .NET
I'm building a basic search function for my site. Realising that the most useful
I am building a little search function for my site. I am taking my
I am building a search box (input field) which should make a server call
Am building a Book search API using Lucene. I need to index Book Name,Author,
I am building an open search add-on for Firefox/IE and the image needs to
I'm trying to build the search for a Django site I am building, and
Building the same project (without any changes) produces binary different exe-files: some small regions
This is a very basic question. I am building a website using the google

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.