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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.