Combining conditions (session variables) in WHERE clause

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joeltonnberg
Forum Newbie
Posts: 2
Joined: Mon Oct 06, 2008 3:44 am

Combining conditions (session variables) in WHERE clause

Post by joeltonnberg »

Hi,

I'm trying to filter a query using two session variables, however all I get is "Query was empty".

Here is my code:

Code: Select all

$colname1 = "-1";
if (isset($_SESSION['Session1'])) {
  $colname1 = $_SESSION['Session1'];
}
  $colname2 = "-1";
if (isset($_SESSION['Session2'])) {
  $colname2 = $_SESSION['Session2'];
}
mysql_select_db("database");
$query_Result = sprintf("SELECT * FROM Table WHERE Variable1 = %s AND Variable2 = %s", GetSQLValueString($colname1, "text", $colname2, "text"));
$Result = mysql_query($query_Result) or die(mysql_error());
$row_Result = mysql_fetch_assoc($Result);
$totalRows_Result = mysql_num_rows($Result);
?>
PietM
Forum Newbie
Posts: 8
Joined: Mon Oct 06, 2008 2:56 am

Re: Combining conditions (session variables) in WHERE clause

Post by PietM »

Doesn't the function GetSQLValueString return one value? If that's the case, then your query will never have a value for the second %s.
joeltonnberg
Forum Newbie
Posts: 2
Joined: Mon Oct 06, 2008 3:44 am

Re: Combining conditions (session variables) in WHERE clause

Post by joeltonnberg »

that's right, thanks! I added a second GetSQLValueString and it works fine :)
Post Reply