superseba888 Posted July 6, 2007 Share Posted July 6, 2007 Hello, I'm new with PHP/MySQL, and got a problem with a SQL syntax : Code : $sql = "SELECT AUN, denomination, ville FROM aun WHERE loc = $loc AND cp = $cp AND ville = $ville ORDER BY ville"; When the 3 variables ($loc, $cp, $ville) are filled (!= null), no problems. But when there is one missing (when it is == null), I've got an SQL error. I would like to find a way, that the SQL query just "ignore" the empty variables (those == null). Any suggestions about the ways to solve this problem ? Thank you in advance ! Vince Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 6, 2007 Share Posted July 6, 2007 if (isset($variable)) { $condition[]=$variable; } if (isset($variable2)) { $condition[]=$variable2;//edit this } if (isset($variable3)) { $condition[]=$variable3; } if (count($condition)>1) $value=implode('and',$condition); $sql = "SELECT AUN, denomination, ville FROM aun WHERE loc = $loc $value ORDER BY ville"; long but thats the logic but wait ill make another example for you Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 6, 2007 Share Posted July 6, 2007 if (isset($variable)) { $condition.='and value='$variable; } if (isset($variable2)) { $condition.='and value='$variable; } if (isset($variable3)) { $condition.='and value='$variable; } $sql = "SELECT AUN, denomination, ville FROM aun WHERE loc = $loc $condition ORDER BY ville"; pls try to debug if threres error Quote Link to comment Share on other sites More sharing options...
superseba888 Posted July 6, 2007 Author Share Posted July 6, 2007 Thanks this looks great. This could be suitable if the conditions are several conditions for "loc". And not if there are some other conditions like cp = $cp and ville = $ville... However, you helped me to understand some php functions (isset, implode, and so on ...) Many thanks ! if (isset($variable)) { $condition[]=$variable; } if (isset($variable2)) { $condition[]=$variable2;//edit this } if (isset($variable3)) { $condition[]=$variable3; } if (count($condition)>1) $value=implode('and',$condition); $sql = "SELECT AUN, denomination, ville FROM aun WHERE loc = $loc $value ORDER BY ville"; long but thats the logic but wait ill make another example for you Quote Link to comment Share on other sites More sharing options...
superseba888 Posted July 6, 2007 Author Share Posted July 6, 2007 Could be this : $condition.='and value='$variable; written like this : $condition = $condition.'and value='$variable; (a sort of $condition + 'and value='$variable) If yes, I think I've got the solution to my problem ... May I please ask you to advise ? Thanks, if (isset($variable)) { $condition.='and value='$variable; } if (isset($variable2)) { $condition.='and value='$variable; } if (isset($variable3)) { $condition.='and value='$variable; } $sql = "SELECT AUN, denomination, ville FROM aun WHERE loc = $loc $condition ORDER BY ville"; pls try to debug if threres error Quote Link to comment 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.