Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 9    Views: 72

GoNorthWest
buzztouch Evangelist
Profile
Posts: 8197
Reg: Jun 24, 2011
Oro Valley, AZ
1,000,000
04/03/13 10:43 PM (12 years ago)

PHP Syntax Question

Hello! @David has been helping me with a php query to a mysql database, but I'm stuck on the php syntax for one of the dynamic queries. Since he's heads down on improving buzztouch, and I'm anxious to get this nailed, I thought I'd post it. Here's the query: $dynamicWhere .= " AND division_id = '" . $_GET["division"] . "' AND home_team = '" . $_GET["team"] . "' OR visitor_team = '" . $GET["team"] . "'"; This is all on a single line. The query is basically this: AND (`home_team` = "2" OR `visitor_team` = "2") What I'm confused about here is how to write the php to make the OR condition. I need to put parenthesis in there somehow, but no matter what I do, the only result I get back is where the home_team equals the team number I pass it. So, it's ignoring the OR clause and just returning the first part of the condition. Any ideas on how to make this work? Again, this is just a syntax issue...the query itself is solid, assuming I can get the OR clause to be recognized. Thanks! Mark
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
04/03/13 10:58 PM (12 years ago)
Can you put the last part at the beginning, so that the AND clause is after the OR?
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
04/03/13 10:59 PM (12 years ago)
http://stackoverflow.com/questions/8604380/mysql-or-condition may have a hint. I've found most of my 'odd' queries by using the PHPMyAdmin control panel; select the table you want to query, and choose the 'sql' tab. It should allow you to almost 'point and click' your query. You can run it from there, and if it errors, the error is right in front of you. Hope it helps! Cheers! -- Smug
 
GoNorthWest
buzztouch Evangelist
Profile
Posts: 8197
Reg: Jun 24, 2011
Oro Valley, AZ
1,000,000
like
04/03/13 11:07 PM (12 years ago)
Thanks for the suggestions! It's not the actual query itself that's the problem...it's how it's represented with php that's causing me an issue. Because variables are being dynamically inserted via a URL, the php has to be written in such a way that it grabs the values for parts of the query, and ultimately forms the correct string. What's stumping me is how to get the parenthesis of the OR clause to be recognized. Thanks! Mark
 
Dusko
Veteran developer
Profile
Posts: 998
Reg: Oct 13, 2012
Beograd
22,680
like
04/03/13 11:23 PM (12 years ago)
Break it down into a series of separate logical variables and if clauses.
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
04/04/13 12:09 AM (12 years ago)
I use double quotes for the php, and single quotes within the query. Hard to post here; the editor sometimes changes things, but you're welcome to snoop through my code for smugcopy or the copy/paste script David and I did over at BT Mods... https://www.buzztouchmods.com/downloads.php?cat_id=2&download_id=49 https://www.buzztouchmods.com/downloads.php?cat_id=2&download_id=48 Just look for "$strSql" which is usually my SQL Query string variable. a good example would be in the copy screen utility, in the file called 'step1.php' in line 76 or so... $strSql = " SELECT * FROM " . TBL_APPLICATIONS . " WHERE guid != '$guid' AND status != 'deleted' ORDER BY dateStampUTC DESC"; One field is called 'guid' and the other field is 'status'. It's sorted (descending) by field 'dateStampUTC'. I don't quote Field names in the query. Basically 'this' query is after you select the source app to copy from, it gets the other 'eligible' apps by asking for anything but the source app, or a deleted app (which actually doesn't get deleted; just earmarked) Cheers! -- Smug
 
GoNorthWest
buzztouch Evangelist
Profile
Posts: 8197
Reg: Jun 24, 2011
Oro Valley, AZ
1,000,000
like
04/04/13 12:27 AM (12 years ago)
Hey Smug, Good idea...thanks! I'll give that a shot and see if it helps my situation. Mark
 
GoNorthWest
buzztouch Evangelist
Profile
Posts: 8197
Reg: Jun 24, 2011
Oro Valley, AZ
1,000,000
like
04/04/13 08:46 AM (12 years ago)
Well, this is extremely embarrassing. The problem was my last GET statement. It should be $_GET, but I had it as just $GET. Fixed that, and all is well. Thanks! Mark
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
04/04/13 02:09 PM (12 years ago)
I see it; and I never noticed it either. Cheers! -- Smug
 
WebNevees
Code is Art
Profile
Posts: 206
Reg: Oct 28, 2012
KL
11,660
like
04/05/13 01:14 AM (12 years ago)
I'm glad you solved the problem, but nevertheless it looks like the query you have made is not the one you desire. Your logic at: AND (`home_team` = "2" OR `visitor_team` = "2") is not carried out in: $dynamicWhere .= " AND division_id = '" . $_GET["division"] . "' AND home_team = '" . $_GET["team"] . "' OR visitor_team = '" . $_GET["team"] . "'"; because you don't use the parentheses and AND has precedence over OR. So it will play out wrong in some cases (not all). Maybe you'd like to consider the following (just adding the parentheses): $dynamicWhere .= " AND division_id = '" . $_GET["division"] . "' AND (home_team = '" . $_GET["team"] . "' OR visitor_team = '" . $_GET["team"] . ")'"; I's usually best to think of ANDs as a X (something times something, like 0 times 1=0) and ORs as a + (something plus something, like 0+1=1).
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.