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.

<?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");

 

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

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.