Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/239704-how-do-you-use-a-variable-operand/
Share on other sites

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.