michael789
asked on
Dynamic ORDER BY
I need to order mySQL query by dynamic GET parameters however it is not working.
$colname_listmain = "-1";
if (isset($_SESSION['kt_login _id'])) {
$colname_listmain = (get_magic_quotes_gpc()) ? $_SESSION['kt_login_id'] : addslashes($_SESSION['kt_l ogin_id']) ;
}
$navletter_listmain = "%";
if (isset($_GET['navletter']) ) {
$navletter_listmain = (get_magic_quotes_gpc()) ? $_GET['navletter'] : addslashes($_GET['navlette r']);
}
$neworder = "%";
if (isset($_GET['neworder'])) {
$neworder = (get_magic_quotes_gpc()) ? $_GET['neworder'] : addslashes($_GET['neworder ']);
}
$sorting2 = "%";
if (isset($_GET['neworder'])) {
$sorting2 = (get_magic_quotes_gpc()) ? $_GET['sorting2'] : addslashes($_GET['sorting2 ']);
}
mysql_select_db($database_ inburn, $inburn);
$query_listmain = sprintf("SELECT * FROM list_asso LEFT JOIN listmain ON listmain.id_list = list_asso.mandantID2 WHERE list_asso.mitarbeiterid2 = %s AND listmain.active = 1 AND listmain.mandantnu2 LIKE %s ORDER BY $neworder $sorting2", GetSQLValueString($colname _listmain, "int"),GetSQLValueString($ navletter_ listmain . "%", "text"));
$query_limit_listmain = sprintf("%s LIMIT %d, %d", $query_listmain, $startRow_listmain, $maxRows_listmain);
$listmain = mysql_query($query_limit_l istmain, $inburn) or die(mysql_error());
$row_listmain = mysql_fetch_assoc($listmai n);
Is it not possible to take two dynamic ORDER by parameters?
Thank you
$colname_listmain = "-1";
if (isset($_SESSION['kt_login
$colname_listmain = (get_magic_quotes_gpc()) ? $_SESSION['kt_login_id'] : addslashes($_SESSION['kt_l
}
$navletter_listmain = "%";
if (isset($_GET['navletter'])
$navletter_listmain = (get_magic_quotes_gpc()) ? $_GET['navletter'] : addslashes($_GET['navlette
}
$neworder = "%";
if (isset($_GET['neworder']))
$neworder = (get_magic_quotes_gpc()) ? $_GET['neworder'] : addslashes($_GET['neworder
}
$sorting2 = "%";
if (isset($_GET['neworder']))
$sorting2 = (get_magic_quotes_gpc()) ? $_GET['sorting2'] : addslashes($_GET['sorting2
}
mysql_select_db($database_
$query_listmain = sprintf("SELECT * FROM list_asso LEFT JOIN listmain ON listmain.id_list = list_asso.mandantID2 WHERE list_asso.mitarbeiterid2 = %s AND listmain.active = 1 AND listmain.mandantnu2 LIKE %s ORDER BY $neworder $sorting2", GetSQLValueString($colname
$query_limit_listmain = sprintf("%s LIMIT %d, %d", $query_listmain, $startRow_listmain, $maxRows_listmain);
$listmain = mysql_query($query_limit_l
$row_listmain = mysql_fetch_assoc($listmai
Is it not possible to take two dynamic ORDER by parameters?
Thank you
ASKER
but $sorting2 has to be either DESC or ASC
I tried that and the query gives an error
I tried that and the query gives an error
Ah sorry, didn't realize it wasn't two sorting fields.
OK, lets try again:
ORDER BY $neworder $sorting2", GetSQLValueString($coln... .. <------ something is wrong here. When these variables are resolved, it's not a correct php syntax.
Should it be something like this:
ORDER BY $neworder $sorting2, " . GetSQLValueString($colname _listmain, "int") . ", " . GetSQLValueString($navlett er_listmai n . "%", "text"));
OK, lets try again:
ORDER BY $neworder $sorting2", GetSQLValueString($coln...
Should it be something like this:
ORDER BY $neworder $sorting2, " . GetSQLValueString($colname
ASKER
hi..i tried that but I am getting this error for this line
Warning: sprintf() [function.sprintf]: Too few arguments in /home/content/66/6620566/h tml/dashbo ard4.php on line 93
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 'LIMIT 0, 10' at line 1
Warning: sprintf() [function.sprintf]: Too few arguments in /home/content/66/6620566/h
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 'LIMIT 0, 10' at line 1
That's an entirely different query and error, so apparently my solution solved the first one.
Check your syntax with all the variables set to values and see if they syntax is correct that way.
Check your syntax with all the variables set to values and see if they syntax is correct that way.
ASKER
actually it does not because the error refers to the line I applied the changes. Don't worry. I somehow try to figure it out
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ORDER BY $neworder, $sorting2",