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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:45:32+00:00 2026-05-31T10:45:32+00:00

I am trying to use variables in my mysql statement so they will change

  • 0

I am trying to use variables in my mysql statement so they will change depending on the input. For example if the user does not enter size, that part of the statement won’t show up. Sorry I am bad at explaining. But when I run the code below, I get an error, but If I remove the ‘dynamic’ AND statements, the code works. I just can’t figure out what I am doing wrong. And ideas.

        if($size2 ==''){
    $size_stat = '';
    }else{
    $size_stat = "AND size='$size2'";

    }

    if($re2 ==''){
    $re_stat = '';
    }else{
    $re_stat = "AND re='$re2'";

    }


    if($status2 ==''){
    $status_stat = '';
    }else{
    $status_stat = "AND status='$status2' ";

    }


    $result2 = mysql_query("SELECT 
      lat,lng,id,re,re_num,name,city,state,zip,address,status,category,size,
      ( 3959 * acos( cos( radians($lat2) ) 
                   * cos( radians( lat ) ) 
                   * cos( radians( lng ) - radians($long2) ) 
                   + sin( radians($lat2) ) 
                   * sin( radians( lat ) ) 
                   )
      ) AS distance 
    FROM 
      locations
    WHERE 
      (state='PA' OR state='NY') 
    .$re_stat. 
     $size_stat.  
     $status_stat. 
    HAVING 
      distance < 300
    ORDER BY 
      distance 
    LIMIT 
      0 , 50    ");

    while($array = mysql_fetch_assoc($result2)){


    $array_json[] = $array;
    }


    echo json_encode($array_json);
  • 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-31T10:45:34+00:00Added an answer on May 31, 2026 at 10:45 am

    You have spacing issues with the AND statements.

    For all your *_stat variables add a space at the beginning and at the end of the string so the sql query does not break.

    For example:

    $size_stat = " AND size='$size2' ";
    

    Also, make sure you have spaces between the “WHERE” and “HAVING” clauses.

    UPDATE: Also, check if the query is actually returning any results.

    UPDATE2: Replace the dots ‘.’ in your query with spaces…

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

Sidebar

Related Questions

I am trying to use Unicode variable names in g++, but it does not
I am trying to use the truncate filter on 2 variables (rsstitle and rssdescription),
I'm trying to read variables from a batch file for later use in the
I'm trying to learn about condition variables and how to use it in a
I am trying to use the below to update a MySQL DB. I connect
I'm trying to use a MERGE INTO statement in my php file to update
I'm trying to create a prepared statement in MySQL that takes in a single
I'm trying to use some mysql data in creating part of a variable name
I'm trying to do this IF statement in a mySQL query which I learnt
I am trying to use a variable to get a function in a extended

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.