Jump to content

$_GET['county'] is null or not null and populating sql


brown2005

Recommended Posts

i have

$_GET['county'];

and 

$sql = mysql_query("SELECT * FROM comps ORDER BY date DESC");

 

now what i want to do is

 

say if $_GET['county']; is null

 

$sql = mysql_query("SELECT * FROM comps ORDER BY date DESC");

 

and if not null

 

$sql = mysql_query("SELECT * FROM comps WHERE county='$_GET['county']' ORDER BY date DESC");

 

what is the easiest way to do this please.

Link to comment
Share on other sites

<?php if (isset($_GET['county']) && $_GET['county'] != '') {
$sql = mysql_query("SELECT * FROM comps WHERE county='$_GET['county']' ORDER BY date DESC");
}  else {
$sql = mysql_query("SELECT * FROM comps ORDER BY date DESC");
}

 

 

 

<?php


$country = $_GET['county'];

$sql_stuff = "SELECT * FROM comps "; //note the space

if (isset($country) && $country != '') {
$sql_stuff .= "WHERE county='$_GET['county']' "; //note the space
}

$sql_stuff .= "ORDER BY date DESC";

$sql = mysql_query($sql_stuff);

 

 

 

Not sure if this will work (but it should do):

<?php
$sql = (isset($_GET['county']) && $_GET['county'] != '') ? mysql_query("SELECT * FROM comps WHERE county='$_GET['county']' ORDER BY date DESC") : mysql_query("SELECT * FROM comps ORDER BY date DESC");

 

Link to comment
Share on other sites

The first and last one I posted are probably quickest (very marginally as lonewolf217 said)...

 

since they do two (mandatory) comparisons and one string assign...

 

 

where as the middle one does one string assign, two (mandatory) comparisons and then a string append.

 

IDK?

 

make them loop about 10000x then compare the time it takes to execute :D

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.