1

I have a sql statement in my php page that looks like this:

 $places=array('CHI','DET','LA','NYC','DALLAS');

$SQL="SELECT NAME,
             ID,
             PHONE,
             EMAIL,
             LOCATION
      FROM SHOPPERS
      WHERE LOCATION IN '{$places}'
      AND ID BETWEEN '25687' AND '28050'
      ";

Then I'm echoing out the query as a table, but I'm not getting any results because there's a problem with the $places array. Any help would be appreciated. Thank you

1 Answer 1

2

You need to format it to be friendly with what SQL expects:

$places=array('CHI','DET','LA','NYC','DALLAS');

$SQL="SELECT NAME,
             ID,
             PHONE,
             EMAIL,
             LOCATION
      FROM SHOPPERS
      WHERE LOCATION IN ('".implode("', '", $places)."')
      AND ID BETWEEN '25687' AND '28050'
      ";

SQL where column in clauses need brackets around them, so you need to add them in - and then you still need to implode the array into a string. The constructed SQL should look like this:

$SQL="SELECT NAME,
             ID,
             PHONE,
             EMAIL,
             LOCATION
      FROM SHOPPERS
      WHERE LOCATION IN ('CHI','DET','LA','NYC','DALLAS')
      AND ID BETWEEN '25687' AND '28050'
      ";
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I tried implode earlier, but I guess I didn't have the right apostrophes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.