MySQL Coding Help (AND/OR statements) |
|
Chris
Joined: Sep 27 2004 Posts: 1564 Location: NY 1993 Mitsubishi Eclipse Last updated: 11/06/06 1984 Mazda RX-7 Last updated: 07/24/07 |
Are you sure you want to delete this post?
So here is the deal;
I have this code
That I ideally want it to do this, but it isn't working; There is a make of a product, Flowlighting. There is a series, Inferno. There is a product, Underbody Kit. What I want it to do is make it so I can have a call to function that grabs something like this: get_info.php?make=Flowlighting&product=Underbody Kit&series=Inferno OR get_info.php?make=Flowlighting&product=Underbody Kit OR get_info.php?make=Flowlighting&series=Inferno It works, kinda. With the first and second OR's are removed, I can call all 3 things and it picks them out. When they are placed in and the code calls for all 3, the entire products/series show up for a certain make. When the code calls for only 2, nothing shows. Here is an example that is live on my website. I am calling for all 3 things, Make = TMA, Product = Subwoofers, Series = T-Series http://shoandgo.com/stereo/get_info.php?make=TM...t=Subwoofers&series=T-Series All TMA products show up. T-Series is for Speakers and Subwoofers, which is why I need to include the &product. If &product or &series is removed, nothing shows up. Whats the deal, y0!? Thanks! Chris |
|
Brandon
Joined: Jun 04 2003 Posts: 3816 Location: St. Louis, MO 1994 Mazda MX-3 Last updated: 09/06/06 2005 Geo Metro Last updated: 06/16/04 2006 Subaru Legacy Last updated: 06/18/06 |
Are you sure you want to delete this post?
Your SQL is heading down the right path, but you need to build your SQL differently.
Try some PHP like this:
A couple other things to notice: "WHERE 1=1" is a great to use when building an AND clause. 1=1 will always return true and makes it really easy to add a bunch of other criteria to the clause. mysql_real_escape_string is required. If you aren't using it then you have a serious vulnerability in your website. If something is going into your database, you need to use mysql_real_escape_string or another strict validation function like intval()/floatval() depending on the datatype. The SELECT * should also be rewritten with specific columns, but its not life or death. |
|
zanson
Joined: Feb 28 2007 Posts: 663 2006 Scion tC Last updated: 06/07/08 |
Are you sure you want to delete this post?
Try this... seems like u had too many ()
i assume all this info is in 1 row of the database?
Brandons code should work as well. |
|
Aken
Joined: Feb 12 2003 Posts: 8760 |
Are you sure you want to delete this post?
Brandon's is the version to use. You don't want your SQL query searching for empty strings. |
|
zanson
Joined: Feb 28 2007 Posts: 663 2006 Scion tC Last updated: 06/07/08 |
Are you sure you want to delete this post?
hummm good point... sorry im a little brain dead today >.< what an off week. |
|
Chris
Joined: Sep 27 2004 Posts: 1564 Location: NY 1993 Mitsubishi Eclipse Last updated: 11/06/06 1984 Mazda RX-7 Last updated: 07/24/07 |
Are you sure you want to delete this post?
Thanks guys.
I'll have to rip brandons code apart so I understand it before I use it. I'll report back in a few days. This is one of those things I work on when I have time....sooo once every other week. |
|
Aken
Joined: Feb 12 2003 Posts: 8760 |
Are you sure you want to delete this post?
Basically he just has three IF statements checking to see if a GET variable exists, and makes sure it isn't empty.
url.php?a=blah&b=stuff a and b are GET variables. $_GET['a'] and $_GET['b'] So after it finds out if a variable is defined in your URL, and makes sure it isn't empty, it adds on to the WHERE clause in your query. |
|
Brandon
Joined: Jun 04 2003 Posts: 3816 Location: St. Louis, MO 1994 Mazda MX-3 Last updated: 09/06/06 2005 Geo Metro Last updated: 06/16/04 2006 Subaru Legacy Last updated: 06/18/06 |
Are you sure you want to delete this post?
only bad thing is if a=0&b=0, they will both be considered "empty". If that's a problem for your script, then change !empty($_GET['make']) to $_GET['make'] !== ''
|
|
Chris
Joined: Sep 27 2004 Posts: 1564 Location: NY 1993 Mitsubishi Eclipse Last updated: 11/06/06 1984 Mazda RX-7 Last updated: 07/24/07 |
Are you sure you want to delete this post?
Thanks guys.
A and B will never be the same. Neither will C...anymore. The problem lied within the TMA, JL Audio and Alpine I believe. They all have subwoofers and speakers with the same series, which I didn't pay any attention to, until I ran across this problem. So lets say I have a call that gets A and B, but not C. Will it still select A and B? If I have a call for A, B and C, will it still work? Thanks! |
|
Brandon
Joined: Jun 04 2003 Posts: 3816 Location: St. Louis, MO 1994 Mazda MX-3 Last updated: 09/06/06 2005 Geo Metro Last updated: 06/16/04 2006 Subaru Legacy Last updated: 06/18/06 |
Are you sure you want to delete this post?
The code I gave you will work for any combination of A B and C.
A AB ABC B BC C and even nothing (if no options are given, every item gets returned) |