Jump to content

SELECT Question


dweb77

Recommended Posts

What is the wildcard or expression for a SELECT statement to get all records in a particular column as shown below.

 

I want all records which have column state='CA' and population greater than 10k and any entry in city. I am processing this from a form where the user can select a particular city or choose 'All' cities. I know how to process the form fields with strings and using GET, but don't know the character to use in the SELECT statement for if they choose 'All'. An example of the final expression with '????' where I am uncertain of what to have the 'All' selection translate to:

 

SELECT location_id FROM locations WHERE $state='CA' AND $city = ???? AND $population > 10000

 

Thank you in advance for any help.

 

 

Link to comment
Share on other sites

first you dont put $ inside the query statement.you append them...

if($city=='ALL')
 $qry="SELECT location_id FROM locations WHERE state='" . $statevariable . "' AND population > " . $populationvariable;
else
 $qry="SELECT location_id FROM locations WHERE state='" . $statevariable . "' AND city= '" . $cityvariable . "' AND population > " . $populationvariable;

Link to comment
Share on other sites

That is completely wrong. You CAN put $ inside a double quoted strings. It acts as a macro in a sense and simply does text replacement.

 

However, you CANNOT use $ inside of single quoted strings. If you try to put $ inside a single quoted string, it will read the $ as a character.

 

GOOD:

<?php
$table = "users";
$query = "SELECT * FROM $table LIMIT 10";
?>

 

BAD:

<?php
$table = "users";
$query = 'SELECT * FROM $table LIMIT 10';
?>

 

<?php
$table = "users";
echo "$table";  // output: users
echo '$table';  // output: $table
?>

Link to comment
Share on other sites

Thank you for the responses...

 

If I were to use the response regarding the if statement, am I just to have several of these as there are four different criteria on the form (I guess I should have included this in the original question). Each could have a general - All - selection or could have a specfic selection.

 

Or is there a better way?

 

Thanks.

 

 

Link to comment
Share on other sites

Another option.  You could also use the ternary operator + sprintf to choose between situations while generating a query in one place.

 

<?php

$query = sprintf('SELECT location_id FROM locations WHERE state="%s"%sAND $population > 10000',
$_POST['state'],
$_POST['city'] == 'ALL' ? '' : " AND city='{$_POST['city']}' "
);

?>

(Error checking omitted for brevity; you should check user submitted data and escape it.)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.