part of query is being ignored

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
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

part of query is being ignored

Post by nite4000 »

I have some code here and it was working fine at one point not sure why it stopped. but its ignoring part of the query after OR and i need all of it to be shown


if maybe there is another way to code the query that would help or tell me what to google for and i will figure it out on my own
Here is the code

Code: Select all

<?php
                
                $q=mysql_query("Select * from support where email='" . $_SESSION['sess_name'] . "' and status='1' OR email='admin@blah.com'and status='4' and id= '$usrid' ORDER BY SENT DESC ")or die(mysql_error());      
            while($info = mysql_fetch_array($q, MYSQL_ASSOC)) {
echo'<tr><td><input type=checkbox name=del[] id=del value='.$info[id].'>
</td><td><img src="../images/email.png"></td>
              <td align="left"><a href="surf_mail.php?id=' . $info['id'] . '">admin@blah.com</a></td>
              <td align="left">'.$info['subject'].'</td>
              <td>'.date("n-d-y",strtotime($info['sent'])).'</td></tr>';
            }
                
        ?>
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: part of query is being ignored

Post by Eric! »

Are you getting an error? I'm not great at sql, but how about adding a space between blah.com'and ?

Code: Select all

              $q=mysql_query("Select * from support where email='" . $_SESSION['sess_name'] . "' and status='1' OR email='admin@blah.com' and status='4' and id= '$usrid' ORDER BY SENT DESC ")or die(mysql_error());
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: part of query is being ignored

Post by nite4000 »

no no error just not getting rows that have the status of 4 and admin@blah.com but the other part works not sure what caused it to stop working.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: part of query is being ignored

Post by Eric! »

Did you see I corrected a syntax problem with your query?
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: part of query is being ignored

Post by nite4000 »

yeah i saw but still not working like it should. its getting the first part where email is ses_name and status=1 but not the rest.

I added info to my profile if you wish to contact me
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: part of query is being ignored

Post by VladSun »

Could you please explain what this:
[sql]here email='" . $_SESSION['sess_name'] . "' AND STATUS='1' OR email='admin@blah.com'AND STATUS='4' AND id= '$usrid'[/sql]
describes in real world?
I'm almost sure you have a precedence issue here.
There are 10 types of people in this world, those who understand binary and those who don't
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: part of query is being ignored

Post by nite4000 »

VladSun wrote:Could you please explain what this:
[sql]here email='" . $_SESSION['sess_name'] . "' AND STATUS='1' OR email='admin@blah.com'AND STATUS='4' AND id= '$usrid'[/sql]
describes in real world?
I'm almost sure you have a precedence issue here.

yeah i will explain it.

This here email='" . $_SESSION['sess_name'] . "' is the email thats logged in with and status='1' is the status of emails the other part after OR is where i send a mass mail to them via saved to db in the same table but is given a default email and a different status which is 4 since i sent it to them and this id= '$usrid' is the id of the record of the user


hope that helps. it did work but not sure why it stopped.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: part of query is being ignored

Post by VladSun »

Obviously, STATUS is a MySQL keyword so escape it like this:
[sql]WHERE email='" . $_SESSION['sess_name'] . "' AND `status`='1' OR email='admin@blah.com' AND `status`='4' AND id= '$usrid'[/sql]

Also try using brackets t express the right precedence:

[sql]WHERE     (        (email='" . $_SESSION['sess_name'] . "' AND `status`='1')        OR         (email='admin@blah.com' AND `status`='4')    )    AND     id= '$usrid'[/sql]
There are 10 types of people in this world, those who understand binary and those who don't
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: part of query is being ignored

Post by nite4000 »

Thanks but didnt work. I think i am gonna just make another page to have it show the status=4 stuff may be alot easier.

Thanks
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

Re: part of query is being ignored

Post by nite4000 »

putting it on another page didnt work something is stopping it from working. You have any other suggestions?
Post Reply