Jump to content

$_Get If Not Working


computermax2328

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.