9999 Posted March 11, 2010 Share Posted March 11, 2010 Hello. Is there anything wrong with this snipet of code? I want to run a query based on a condition set forth in the if-then statment. The first two shown in all green work perfectly. The second two don't work. $cat_id = $_GET['cat_id']; $state = $_GET['state']; if (($cat_id == "00") && ($state == "00")) { [color=green]$query = "SELECT name,address,city,state,zip,phone,web,other FROM ydc ORDER BY name ASC";[/color] } else if (($cat_id != "00") && ($state == "00")) { [color=green]$query = "SELECT name,address,city,state,zip,phone,web,other FROM ydc WHERE cat_id = $cat_id ORDER BY name ASC";[/color] } else if (($cat_id == "00") && ($state != "00")) { [color=red]$query = "SELECT name,address,city,state,zip,phone,web,other FROM ydc WHERE state = $state ORDER BY name ASC";[/color] } else { [color=red]$query = "SELECT name,address,city,state,zip,phone,web,other FROM ydc WHERE cat_id = $cat_id AND state = $state ORDER BY name ASC";[/color] } $result = mysql_query($query) or die('Sorry, could not get the listings at this time, please try later'); if (mysql_num_rows($result) == 0) { echo "<h3>Sorry, there are no listings that match your selection.</h3>"; } else { while($row=mysql_fetch_array($result, MYSQL_ASSOC)) { $name = $row['name']; $address = $row['address']; $city = $row['city']; $state = $row['state']; $zip = $row['zip']; $phone = $row['phone']; $web = $row['web']; $other = $row['other']; echo "<h3>$name</h3>"; echo "$address<br />"; echo "$city, $state $zip<br />"; echo "$phone<br />"; echo "<a href=http://$web>Visit Website</a><br />"; echo "$other"; echo "<h2> </h2>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/194860-help-4-queries-within-an-if-then-else-2-work-and-2-dont/ Share on other sites More sharing options...
jdorma0 Posted March 11, 2010 Share Posted March 11, 2010 Why don't you replace die('Sorry, could not get the listings at this time, please try later'); with die(mysql_error()); Just to see if you get any errors on those other two conditions. Quote Link to comment https://forums.phpfreaks.com/topic/194860-help-4-queries-within-an-if-then-else-2-work-and-2-dont/#findComment-1024570 Share on other sites More sharing options...
9999 Posted March 11, 2010 Author Share Posted March 11, 2010 Thanks. Will try. Do you see any other problems? Quote Link to comment https://forums.phpfreaks.com/topic/194860-help-4-queries-within-an-if-then-else-2-work-and-2-dont/#findComment-1024571 Share on other sites More sharing options...
jdorma0 Posted March 11, 2010 Share Posted March 11, 2010 Thanks. Will try. Do you see any other problems? Well, I actually created a live test of your code on my server and it's operating exactly as intended. http://www.widocomputers.com/smarttime/EmptyPHP.php Just click the different conditions at the bottom of the page to see it in action. I'm not using the queries though, I'm just testing the conditions. Quote Link to comment https://forums.phpfreaks.com/topic/194860-help-4-queries-within-an-if-then-else-2-work-and-2-dont/#findComment-1024573 Share on other sites More sharing options...
9999 Posted March 11, 2010 Author Share Posted March 11, 2010 I get Unknown column 'AL' in 'where clause' I get 'AL' here but whatever state I select shows. Does that mean that there is some prob in the db itself? Quote Link to comment https://forums.phpfreaks.com/topic/194860-help-4-queries-within-an-if-then-else-2-work-and-2-dont/#findComment-1024574 Share on other sites More sharing options...
jdorma0 Posted March 11, 2010 Share Posted March 11, 2010 $cat_id = $_GET['cat_id']; $state = $_GET['state']; if (($cat_id == "00") && ($state == "00")) { $query = "SELECT name,address,city,state,zip,phone,web,other FROM ydc ORDER BY name ASC"; } else if (($cat_id != "00") && ($state == "00")) { $query = "SELECT name,address,city,state,zip,phone,web,other FROM ydc WHERE cat_id = $cat_id ORDER BY name ASC"; } else if (($cat_id == "00") && ($state != "00")) { $query = "SELECT name,address,city,state,zip,phone,web,other FROM ydc WHERE state = '$state' ORDER BY name ASC"; } else { $query = "SELECT name,address,city,state,zip,phone,web,other FROM ydc WHERE cat_id = $cat_id AND state = '$state' ORDER BY name ASC"; } $result = mysql_query($query) or die(mysql_error()); if (mysql_num_rows($result) == 0) { echo "<h3>Sorry, there are no listings that match your selection.</h3>"; } else { while($row=mysql_fetch_array($result, MYSQL_ASSOC)) { $name = $row['name']; $address = $row['address']; $city = $row['city']; $state = $row['state']; $zip = $row['zip']; $phone = $row['phone']; $web = $row['web']; $other = $row['other']; echo "<h3>$name</h3>"; echo "$address<br />"; echo "$city, $state $zip<br />"; echo "$phone<br />"; echo "<a href=http://$web>Visit Website</a><br />"; echo "$other"; echo "<h2> </h2>"; } } Try that. I think that will work because you have to use quotes whenever the data is not a integer. Quote Link to comment https://forums.phpfreaks.com/topic/194860-help-4-queries-within-an-if-then-else-2-work-and-2-dont/#findComment-1024576 Share on other sites More sharing options...
9999 Posted March 11, 2010 Author Share Posted March 11, 2010 Thanks. I will try this and look at the db. Quote Link to comment https://forums.phpfreaks.com/topic/194860-help-4-queries-within-an-if-then-else-2-work-and-2-dont/#findComment-1024577 Share on other sites More sharing options...
jdorma0 Posted March 11, 2010 Share Posted March 11, 2010 Thanks. I will try this and look at the db. No problem. Let me know if it works or if it helped. Quote Link to comment https://forums.phpfreaks.com/topic/194860-help-4-queries-within-an-if-then-else-2-work-and-2-dont/#findComment-1024579 Share on other sites More sharing options...
9999 Posted March 11, 2010 Author Share Posted March 11, 2010 Worked perfectly. Thank you very much!!! SOLVED! Quote Link to comment https://forums.phpfreaks.com/topic/194860-help-4-queries-within-an-if-then-else-2-work-and-2-dont/#findComment-1024580 Share on other sites More sharing options...
jdorma0 Posted March 11, 2010 Share Posted March 11, 2010 You're welcome! Let me know if you need anything else. Quote Link to comment https://forums.phpfreaks.com/topic/194860-help-4-queries-within-an-if-then-else-2-work-and-2-dont/#findComment-1024582 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.