Simple SELECT * where id IS NOT EQUAL TO....???

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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Simple SELECT * where id IS NOT EQUAL TO....???

Post by simonmlewis »

Code: Select all

$rejectoffer = mysql_query ("SELECT * FROM offers WHERE sellerprodid = '$sellerprodid' AND buyerprodid <> '$buyerprodid'");
This is not being liked by my machine.
I thought <> or != would work.

How do you state that you want to find items in the table where something is NOT EQUAL to something.

So if I pass "3" to a database, and I want everything that isn't a 3...........??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Simple SELECT * where id IS NOT EQUAL TO....???

Post by simonmlewis »

Here's all the code.
My error (shown further below) is about line 48 and 65.
48 is "while($rowoffer.....)"
65 is "mysql_free_result($result2);".

Code: Select all

$rejectoffer = mysql_query ("SELECT * FROM offers WHERE sellerprodid = '$sellerprodid' AND buyerprodid != $buyerprodid");
while ($rowoffer = mysql_fetch_object($rejectoffer))
      {
      $result2 = mysql_query ("SELECT email, firstname, lastname FROM users WHERE id == '$rowoffer->buyerid'");
while ($row2 = mysql_fetch_object($result2))
      {
      $to = "$row2->email";
      $subject =  "domain Rejected";
      $headers = "From: noreply@domain.co.uk";
      $body = "** This email account is not monitored - please do not reply. **
        
Dear $row2->firstname $row2->lastname
 
We are sorry to inform you that an offer you have made has been rejected.  Please login to see which item you are being emailed about.
 
Best regards
";
        mail ($to, $subject, $body, $headers);
      }
      mysql_free_result($result2);
      }
      mysql_free_result($rejectoffer);
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Simple SELECT * where id IS NOT EQUAL TO....???

Post by social_experiment »

Try :

Code: Select all

<?php $rejectoffer = mysql_query ("SELECT * FROM offers WHERE sellerprodid = '$sellerprodid' AND buyerprodid NOT LIKE '$buyerprodid'"); ?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Simple SELECT * where id IS NOT EQUAL TO....???

Post by simonmlewis »

Sorry same error.
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /usr/local/psa/home/vhosts/domain.co.uk/httpdocs/beta/includes/myoffers.inc on line 48

Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /usr/local/psa/home/vhosts/domain.co.uk/httpdocs/beta/includes/myoffers.inc on line 65
In the place.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Simple SELECT * where id IS NOT EQUAL TO....???

Post by Eran »

The error has nothing to do with the comparison operator. Your original attempt was correct, you should be using != or <> which both indicate "not matching".
You should be checking the result of a mysql_query() before you pass it on to other functions -

Code: Select all

if($rejectoffer === false) {
    echo mysql_error();
}
Also, echo out the query string just before you run mysql_query() on it and put it here (my guess is that one of the variables is not set)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Simple SELECT * where id IS NOT EQUAL TO....???

Post by simonmlewis »

I am sorry but I don't understand - mainly about the little but of 'error' code you gave me.
This is the full code that is called upon if a variable has "accept" in it.
It basically tells the one successful person they have got something, and ALL OTHERS in the list that they were rejected.
It's the REJECTED part that's causing the errors.

Code: Select all

     $offer = mysql_query ("SELECT * FROM offers WHERE id = '$offerid'");
while ($rowoffer = mysql_fetch_object($offer))
      {
$result = mysql_query ("SELECT email, firstname, lastname FROM users WHERE id = '$rowoffer->buyerid'");
while ($row = mysql_fetch_object($result))
         {
        $to = "$row->email";
        $subject =  "domain Offer Accepted";
        $headers = "From: noreply@domain.co.uk";
        $body = "** This email account is not monitored - please do not reply. **
        
Dear $row->firstname $row->lastname
 
An offer you have made on domain has been accepted.  Please login to see which item you are being emailed about, and take necessary steps to despatch your item for theirs.
 
Best regards
domain
http://www.domain.co.uk
";
        mail ($to, $subject, $body, $headers);
        }
mysql_free_result($result);
      }
mysql_free_result($offer);
 
$rejectoffer = mysql_query ("SELECT * FROM offers WHERE sellerprodid = '$sellerprodid' AND buyerprodid <> '$buyerprodid'");
while ($rowoffer = mysql_fetch_object($rejectoffer))
      {
      $result2 = mysql_query ("SELECT email, firstname, lastname FROM users WHERE id == '$rowoffer->buyerid'");
while ($row2 = mysql_fetch_object($result2))
      {
      $to = "$row2->email";
      $subject =  "domain Offer Rejected";
      $headers = "From: noreply@domain.co.uk";
      $body = "** This email account is not monitored - please do not reply. **
        
Dear $row2->firstname $row2->lastname
 
We are sorry to inform you that an offer you have made on domain has been rejected.  Please login to see which item you are being emailed about.
 
Best regards
domain
http://www.domain.co.uk
";
        mail ($to, $subject, $body, $headers);
      }
      mysql_free_result($result2);
      }
      mysql_free_result($rejectoffer);
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Simple SELECT * where id IS NOT EQUAL TO....???

Post by social_experiment »

Code: Select all

<?php $result2 = mysql_query ("SELECT email, firstname, lastname FROM users WHERE id = '$rowoffer->buyerid'");
while ($row2 = mysql_fetch_object($result2)) ?>
Remove the '==' and make it only '=' in the $result2 query.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Simple SELECT * where id IS NOT EQUAL TO....???

Post by simonmlewis »

Now that has surprised me.
I thought the code at the be "is equal to"...."==", because else you have to do <>, != etc....

Why does = work and == not work?

By the way - WORKS A TREAT.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Simple SELECT * where id IS NOT EQUAL TO....???

Post by social_experiment »

Im not sure, maybe the '==' is not part of the SQL syntax.

http://dev.mysql.com/doc/refman/5.0/en/ ... ators.html
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply