sptrsn Posted June 18, 2011 Share Posted June 18, 2011 I want to use greater than or less than in my select statement depending on the user. I have no problem with the conditional statement and can echo the variable just fine. I just can't figure out how to use it in the select statement. For context, here is a simplified condition. (which is working fine) if ($user_id == 50) { $oper == '<'; } else { $oper == '>'; } The problem comes when trying to use that variable ($oper) in the select statement. $sql = "select * from t1 where v1 '$oper' v2"; Can some kind soul tell me what I'm doing wrong, or point me to some documentation? Thanks a bunch. mysql v 5.0.51a php5.2.4 Quote Link to comment https://forums.phpfreaks.com/topic/239704-how-do-you-use-a-variable-operand/ Share on other sites More sharing options...
Pikachu2000 Posted June 18, 2011 Share Posted June 18, 2011 Don't quote the variable holding the operator. Quote Link to comment https://forums.phpfreaks.com/topic/239704-how-do-you-use-a-variable-operand/#findComment-1231343 Share on other sites More sharing options...
sptrsn Posted June 18, 2011 Author Share Posted June 18, 2011 That works great. Thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/239704-how-do-you-use-a-variable-operand/#findComment-1231346 Share on other sites More sharing options...
mikesta707 Posted June 18, 2011 Share Posted June 18, 2011 To add to what pikachu said, when trying to figure out mysql errors, it is usually helpful to output the query when the mysql fails. For example $result = mysql_query($sql) or die("There was a mysql error: " . mysql_error() . "<br />On the query: " . $sql); Or die is kind of a touchy subject, as its not good practice when your site is live, so you will want to switch to a more elegant solution to handling errors, but while debugging this is a great tool. It allows you to view the query as its been sent, and what the error is. This allows you to catch simple syntax errors like yours. Quote Link to comment https://forums.phpfreaks.com/topic/239704-how-do-you-use-a-variable-operand/#findComment-1231347 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.