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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:30:35+00:00 2026-06-11T14:30:35+00:00

Im trying to come up with MySQL logic for a search function I got

  • 0

Im trying to come up with MySQL logic for a search function I got on my page. Its a simple form where the user can choose to fill in search criteria. The criteria(s) is send as arguments to a function that generates the mysql logic. This is whats inside the PHP controller file:

   case 'search':
         if((empty($_POST['username'])) && (empty($_POST['firstname'])) && (empty($_POST['lastname']))
                && (empty($_POSt['agemin'])) && (empty($_POST['agemax'])) && (empty($_POST['country']))){
                $members = get_all_username();
         } else {
            if(isset($_POST['username'])){
                $otheruser = $_POST['username'];
            } else { $otheruser = null; }
            if(isset($_POST['agemin'])){
                $ageMin = $_POST['agemin'];
            } else { $ageMin = null; }
            if(isset($_POST['agemax'])){
                $ageMax = $_POST['agemax'];
            } else { $ageMax = null; }
            if(isset($_POST['country'])){
                $country = $_POST['country'];
            } else { $country = null; }
            //if(isset($_POST['isonline']))

        $members = search_members($otheruser, $ageMin, $ageMax, $country);
        }
        include('displaySearch.php');
        break;

So if nothing is set a complete list of all the members is generated and displayed. This is the function that is called if any of the inputs is set:

function search_members($username, $ageMin, $ageMax, $country){
    global $db;
    $query = "SELECT username FROM profiles WHERE username = :username 
        AND age > :ageMin AND age < :ageMax AND country = :country";
    $statement = $db->prepare($query);
    $statement->bindValue(':username', $username); $statement->bindValue(':ageMin', $ageMin);
    $statement->bindValue(':ageMax', $ageMax); $statement->bindValue(':country', $country);
    $statement->execute();
    if($statement->rowCount() >= 1){
        return $statement->fetchAll();
    } else {
        return false;
    }
}

The mysql logic is obviously wrong. I need a set of conditions (in the MySQL logic if possible) that checks the PHP variables for value and if there is none it should not be accounted for when querying the database. So if only the username is set in the form the other variables should not be included in the SQL logic.

I’ve looked up the MySQL IF() condition but Im still not able to come up with proper code that does what I need. If someone could point me in the right direction I would be able to do the rest myself. Any other approach for solving this kind of problem is also welcome.

  • 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-11T14:30:36+00:00Added an answer on June 11, 2026 at 2:30 pm

    If i understand your problem, then the simple way is to use if else to build sql query, for example

    $sql = "SELECT username FROM profiles WHERE 1 "
    if (!is_null($username)) {
        $sql .= " AND username = :username ";
    }
    // All other checks
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to come up with a method to differentiate between form being
I'm trying to come up with a MySQL query to select the last record
I'm very new to MySQL and I'm trying to come up with a query
I come from MySQL background and I am trying to learn MongoDB. I have
I come from a MySQL background and am trying to wrap my head around
Another problem I've come across is this one. I'm trying to call this function
I am trying to create a page where users can edit information. I can
I'm trying to come up with a MySQL query that will update points ...
I'm trying to form my PHP/MySQL output in a 'Word-like' table format: one row
Hello im trying to make a few random numbers and do a simple mysql

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.