computermax2328 Posted December 21, 2012 Share Posted December 21, 2012 Hello All, First let me say, happy holidays! Second, I am glad to see that you are all still here. I am having trouble passing a variable through the url, getting it and then determining the correct query with it. For example: $find = $_GET['x']; if ($find = 'y') { sql = "SELECT * FROM table1"; } if ($find = 'z') { sql = "SELECT * FROM table2"; } mysql_query($sql, $connection); When I do this and then change the variable in the url it just takes the information from the first if statement. $find if always equal to "y". Any ideas?? Link to comment https://forums.phpfreaks.com/topic/272267-_get-if-not-working/ Share on other sites More sharing options...
cpd Posted December 21, 2012 Share Posted December 21, 2012 Echo the contents of $_GET['x'] to ensure its the expected value. Link to comment https://forums.phpfreaks.com/topic/272267-_get-if-not-working/#findComment-1400821 Share on other sites More sharing options...
DavidAM Posted December 21, 2012 Share Posted December 21, 2012 A single equals-sign "=" is the assignment operator. So the first IF statement is assigning "y" to $find and will always be true. To compare, use the comparison operator, "==" (two equals-signs). Also, you should check to see if the "x" variable is actually set in the URL. Also, if $find is equal to "y" then it can't be equal to "z", so there is no sense in doing the second IF. Use an "ELSE IF" $find = (isset($_GET['x'] ? $_GET['x'] : ''); if ($find == 'y') { sql = "SELECT * FROM table1"; } else if ($find == 'z') { sql = "SELECT * FROM table2"; } else { // Do something here because there is no $sql value since 'x' is not "y" or "z" } mysql_query($sql, $connection); Link to comment https://forums.phpfreaks.com/topic/272267-_get-if-not-working/#findComment-1400822 Share on other sites More sharing options...
computermax2328 Posted December 21, 2012 Author Share Posted December 21, 2012 I will give the "==" a try. As for echoing the value, I have and it is always the same. I will report back Link to comment https://forums.phpfreaks.com/topic/272267-_get-if-not-working/#findComment-1400825 Share on other sites More sharing options...
computermax2328 Posted December 21, 2012 Author Share Posted December 21, 2012 "==" worked. Good call. Why didn't I think of that?? Link to comment https://forums.phpfreaks.com/topic/272267-_get-if-not-working/#findComment-1400828 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.