Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Jake Belk
Starting Member
7 Posts |
Posted - 2012-01-27 : 18:03:43
|
I'm completely new to sql so please bear with me. I'm building a small real estate web site that is linked to my property database. I would like for clients to search by baths and bedrooms( I'm keeping it simple for now) I have 2 drop down lists on the search page that give the client the choice of:Any, 1, 2, 3, etc for each field. How do I structure the sql to let them see Any(All) baths with 2 bedrooms or 2 baths with Any bedrooms? Or even let the client see any bath/ bed combo in the database. What if they don't know and want to see every property in the db? I have tried the AND, OR, NULL, and () operators to get around this. Is this a default value problem? Im using Dreamweaver CS4.SQL CodeSELECT Condos.BathsTotal, Condos.Beds, Condos.City, Condos.ListPrice, Condos.StreetNameFROM CondosWHERE Condos.Beds=varBeds AND Condos.BathsTotal=varBaths PHP Code<?phpif (!function_exists("GetSQLValueString")) {function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue;}}$varBeds_belleResults = "-1";if (isset($_GET['beds'])) { $varBeds_belleResults = $_GET['beds'];}$varBaths_belleResults = "-1";if (isset($_GET['baths'])) { $varBaths_belleResults = $_GET['baths'];}mysql_select_db($database_connBelle, $connBelle);$query_belleResults = sprintf("SELECT Condos.BathsTotal, Condos.Beds, Condos.City, Condos.ListPrice, Condos.StreetName FROM Condos WHERE Condos.Beds=%s AND Condos.BathsTotal=%s", GetSQLValueString($varBeds_belleResults, "int"),GetSQLValueString($varBaths_belleResults, "text"));$belleResults = mysql_query($query_belleResults, $connBelle) or die(mysql_error());$row_belleResults = mysql_fetch_assoc($belleResults);$totalRows_belleResults = mysql_num_rows($belleResults);?>I'm just trying to start small before expanding the search form. ANY help is greatly appreciated.-Jake |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-27 : 18:13:43
|
| isnt OR what you want?WHERE Condos.Beds=varBeds OR Condos.BathsTotal=varBaths------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Jake Belk
Starting Member
7 Posts |
Posted - 2012-01-27 : 19:53:05
|
Thanks for the quick response. I think I have simplified the search to much for this demo. The OR does work, but it doesn't retrieve all data if the field is left empty (value"").Brett, I'm pretty new at this and I have never created a Stored Procedure. I guess I want to know how to create a query that gives the client flexibility to select or not to select a field. But when left empty, any value would be shown. I have given Price a predetermined range ($50,000 - $400,000) Is there a default value that will deliver everything when no value is selected?This was my original sql code:SELECT Condos.BathsTotal, Condos.Beds, Condos.City, Condos.ListPrice, Condos.StreetName, Condos.CountyFROM CondosWHERE Condos.ListPrice < varHp AND Condos.ListPrice > varLp AND Condos.City=varCity AND Condos.County=varCounty AND Condos.Beds=varBeds AND Condos.BathsTotal=varBaths |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Jake Belk
Starting Member
7 Posts |
Posted - 2012-01-27 : 21:47:44
|
| Oh God, I think my head is about to explode. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2012-01-28 : 02:34:34
|
"Any(All) baths with 2 bedrooms or 2 baths with Any bedrooms?"These are examples of TWO separate queries, right?If so then only concatenate to your $query_belleResults the parts for parameters that are usedI don't know the syntax for PHP but something like this:$query_belleResults = sprintf("SELECT Condos.BathsTotal, Condos.Beds, Condos.City, Condos.ListPrice, Condos.StreetName FROM Condos WHERE 1=1");IF $varBeds_belleResults > 0 THEN $query_belleResults = $query_belleResults + " AND Condos.Beds=%s", GetSQLValueString($varBeds_belleResults, "int"));IF $varBaths_belleResults > 0 THEN $query_belleResults = $query_belleResults + " AND Condos.BathsTotal=%s",GetSQLValueString($varBaths_belleResults, "text"));By the by, why is BEDS using GetSQLValueString "int" and BATHS using "text" ??But ... as Brett has said ... there are all sorts of problems with this. Performance is poor, security is poor (read about "SQL Injection") and fixing issues in the database code is difficult - because you have to modify your actual PHP code each time you need to "adjust" the SQL.If you put the SQL in a Procedure instead then you can adjust that procedure if you need to. For example, you decide to put some parts of the Property details into another table in the database. Now you can change the procedure(s) that now need to access that second table and you don't have to change your PHP code at all. (Its not a good example, but such database structural changes happen in-the-real-world for all sorts of reasons, and IME it is important that the applciation can be easily adjusted to the database.Having said all that it looks like youa re using MySQL? This is a Microsoft SQL Server forum, so you may find that folk here don't know much about MySQL (I've never used it ...) nor PHP for that matter, so you might be better off asking questions on a forum more specific to MySQL |
 |
|
|
Jake Belk
Starting Member
7 Posts |
Posted - 2012-01-28 : 16:09:20
|
| Thanks for the guidance Kristen. I have only been working with a database for a month. I am using MySql, to be more precise phpMyAdmin. I really thought all Sql was the same. I spent the better part of today searching for mySql Stored Procedure help. I'm just trying to find a tiny example to get me started. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-28 : 17:51:43
|
quote: Originally posted by Jake Belk Thanks for the quick response. I think I have simplified the search to much for this demo. The OR does work, but it doesn't retrieve all data if the field is left empty (value"").Brett, I'm pretty new at this and I have never created a Stored Procedure. I guess I want to know how to create a query that gives the client flexibility to select or not to select a field. But when left empty, any value would be shown. I have given Price a predetermined range ($50,000 - $400,000) Is there a default value that will deliver everything when no value is selected?This was my original sql code:SELECT Condos.BathsTotal, Condos.Beds, Condos.City, Condos.ListPrice, Condos.StreetName, Condos.CountyFROM CondosWHERE Condos.ListPrice < varHp AND Condos.ListPrice > varLp AND Condos.City=varCity AND Condos.County=varCounty AND Condos.Beds=varBeds AND Condos.BathsTotal=varBaths
so you're telling you want ones with blank value regardless of values passed for your parameters?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Jake Belk
Starting Member
7 Posts |
Posted - 2012-02-02 : 14:45:03
|
| Thanks Visakh, I had to take a break. I don't know if this is a PHP or SQL problem.I have three drop lists on a search page: Beds , Baths, City. The default selection for the list would say "Any". Users could leave all lists set to any, and press submit to show every row in the database. OR they could pick 1 Bedroom, Any Bath, Any City, for all the 1 bedrooms. OR 1 Bedroom, 1 bath, Any City for all the 1 bed 1 baths in all cities. It's the "any" Value that has me stumped. Like I said before, I'm completely new and stuck. Any, and I mean any help is greatly appreciated.Since this is a SQL Server forum, can I email you directly? You can reach me at jake at ally contractors dot com.Thanks again |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-02 : 14:54:15
|
| then it should be likeAND (Condos.City=varCity OR varCity='Any')AND (Condos.County=varCounty OR varCounty='Any'AND (Condos.Beds=varBeds OR varBeds='Any' AND (Condos.BathsTotal=varBaths OT varBaths='Any'------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Jake Belk
Starting Member
7 Posts |
Posted - 2012-02-11 : 19:58:31
|
It worked!!!!!!I really appreciate the time and knowledge that you shared with me. The finished code looks like:SELECT Condos.BathsTotal, Condos.Beds, Condos.City, Condos.ListPrice, Condos.StreetName, Condos.CountyFROM CondosWHERE (Condos.City=varCity OR varCity='Any') AND (Condos.Beds=varBeds OR varBeds='Any') AND (Condos.BathsTotal=varBaths OR varBaths='Any') AND (Condos.County=varCounty OR varCounty='Any')ORDER BY Condos.ListPrice DESC I have several other questions to ask, is there a mysql forum you contribute to Visakh?Thanks again,-Jake |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-12 : 12:07:06
|
I post mostly here. Also MySQL knowledge is very limited. If you're queries are related to MS SQL Server I'll be able to help you more ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|