PDA

View Full Version : variable columns in a query


dmilani
04-22-2007, 03:07 PM
IS it possible to have a variable/string denoting the column in a search expression?

For instance:

<?php
ini_set ("display_errors", "1");
include 'config.php';
include 'opendb.php';
$tabsel = $HTTP_POST_VARS['tabsel'];
$sess = $_HTTP_POST_VARS['session'];
$sorttab = $_GET['sorttab'];

$sortvar = $_GET['sortvar'];

if (empty($sortvar)) {
$sortvar= 'tname' ;
}

if (empty($tabsel)) {
$tabsel= $sorttab ;
}

$query = "SELECT tname, contact, email, reqdiv, day, eve, cell, misc, date FROM $tabsel WHERE $sess = 'yes' ORDER BY '$sortvar' ASC";


Echoing my query reports this:

SELECT tname, contact, email, reqdiv, day, eve, cell, misc, date FROM HSG_Reg06 WHERE = 'yes' ORDER BY 'tname' ASC

Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= 'yes' ORDER BY 'tname' ASC' at line 1

I am confused because $tabsel is replaced as expected but $sess is blank.

firepages
04-22-2007, 03:44 PM
what do you get when you
<?echo $sess;?>

? .. my guess is nothing & thats the problem, you are not checking anywhere if $_POST['session'] is set or what data it contains

kbluhm
04-22-2007, 03:45 PM
$tabsel = $HTTP_POST_VARS['tabsel'];
$sess = $_HTTP_POST_VARS['session'];
Man, this is an eeeeasy one. Extra underscore.

firepages
04-22-2007, 03:48 PM
$tabsel = $HTTP_POST_VARS['tabsel'];
$sess = $_HTTP_POST_VARS['session'];
Man, this is an eeeeasy one. Extra underscore.

lol well spotted :)

dmilani
04-22-2007, 06:13 PM
I hate asking stupid questions!!

Thanks for your time and help!