Jump to content

Query using a OR ?


thefollower

Recommended Posts

I have a query when i need to use AND's along side some OR's on different fields ... but It's not working.. this is the query line i did :

 

$GetChancellor = mysql_query("SELECT * FROM governmentparties
                    WHERE Treasurer='$UserName' OR PartyLeader='$UserName' AND Prime='1' And CountryID='1'")
			or die(mysql_error());

 

When i did a get the row it would not find a row ... there was no error however.

 

I have this for $UserName = User2.

The current result is in the database, the "Treasurer" is User1,

PartyLeader = User2.

 

Link to comment
Share on other sites

Yes, that looks alright. Display the actual query (with values populated). Take that and run the query outside of PHP to see if it works.

 

FYI: It's usually inefficient mixing OR's with AND's and it can slow queries down quiet a bit. I suggest this alternative:

 

SELECT

          *

  FROM

          governmentparties

 

WHERE  1

          AND (Treasurer = '$UserName')

          AND (Prime = '1')

          AND (CountryID = '1')

UNION

SELECT

          *

  FROM

          governmentparties

 

WHERE  1

          AND (PartyLeader = '$UserName')

          AND (Prime = '1')

          AND (CountryID = '1')

;

 

Link to comment
Share on other sites

Is that the correct syntax or you just written it like that for me to understand ? Ive not seen queries written like that before :P

 

Yes, that's the correct syntax.

 

Example:

<?php

// Assign $UserName something

$strQueryFmt = '
            SELECT 
                       * 
              FROM 
                       `governmentparties`
            
            WHERE      1
                       AND `Treasurer` = "%1$s"
                       AND `Prime` = "%2$s"
                       AND `CountryID` = "%3$s"
            UNION
            SELECT 
                       * 
              FROM 
                       `governmentparties`
            
            WHERE      1
                       AND `PartyLeader` = "%1$s"
                       AND `Prime` = "%2$s"
                       AND `CountryID` = "%3$s"
        ';

$strQuery = sprintf(  $strQueryFmt
                    , mysql_real_escape_string($UserName)
                    , 1      // Prime value
                    , 1      // CountryID value
                   );

$objResult = mysql_query($strQuery);

if ($objResult) {
    
    /**
     * If you know you're only getting back one row (or limit the query to 
     * return just one row), then you don't need the while loop.
     */
    while ($objRow = mysql_fetch_object($objResult)) {

        // Process data. Just an example of displaying two column values:
        echo 'Treasurer: ', $objRow->Treasurer, ' PartyLeader: ', $objRow->PartyLeader;
    }
  
    @mysql_free_result($objResult);
    
}

?>

Link to comment
Share on other sites

$strQuery = sprintf(  $strQueryFmt
                    , mysql_real_escape_string($UserName)
                    , 1      // Prime value
                    , 1      // CountryID value
                   );

$objResult = mysql_query($strQuery);

 

what is this part doing =/

Link to comment
Share on other sites

why cant AND `Treasurer` = "%1$s"

 

just be AND `Treasurer` = "$Username"

like i usually do ?

You can. FYI - Any variable value obtained from user (like from a web form), especially a string, should be escaped first. I like using the sprintf() function because you can do calculations or call functions before a value is assigned. If you're going to use $Username in double quotes, then I recommend that you escape it before using it in the query.

 

http://us3.php.net/manual/en/function.mysql-real-escape-string.php

http://us3.php.net/manual/en/function.addslashes.php

 

i am so lost lol.. i been learning php/mysql for over a year and never come across mysql like this before.. because i've always just put the $Username straight in .. or what ever the variable is that i need..

No need to feel lost. Just do it any way you want, like or used to.

 

The main thing I was trying to show is an example of using UNION instead of having to use OR's.

 

hth.

 

 

 

Link to comment
Share on other sites

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.