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
https://forums.phpfreaks.com/topic/61542-select-question/
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
https://forums.phpfreaks.com/topic/61542-select-question/#findComment-306295
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
https://forums.phpfreaks.com/topic/61542-select-question/#findComment-306297
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
https://forums.phpfreaks.com/topic/61542-select-question/#findComment-306305
Share on other sites

Use wsantos approach.

<?php
if ($city == "All") {
$query = "SELECT location_id FROM locations WHERE state='$state' AND population>10000";
} else {
$query = "SELECT location_id FROM locations WHERE state='$state' AND city='$city' AND population>10000";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/61542-select-question/#findComment-306309
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
https://forums.phpfreaks.com/topic/61542-select-question/#findComment-306317
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.