让PHP和MySQL读取url

So I want to be able to use a WHERE command in SQL to select certain values from a table. This is my current code however it doesn't work

$MobileNumber = $_GET["MobileNumber"];
$TeamGroup = $_GET["TeamGroup"];

if($_REQUEST=['MobileNumber']) {
    $Item = "SELECT a,b,c,d FROM Item WHERE MobileNumber = $MobileNumber";
} elseif($_REQUEST=['TeamGroup']) {
    $Item = "SELECT a,b,c,d FROM Item WHERE TeamGroup = $TeamGroup";
} else {
    $Item = "SELECT a,b,c,d FROM Item";
};

$result = mysql_query($Item) or die ("Error in query: $query. ".mysql_error());

if (mysql_num_rows($result) > 0) {
    $row = mysql_fetch_assoc($result);
    print '<table><tr>';
    foreach($row as $name => $value) {
        print "<th>$name</th>";
    }
    print '</tr>';

    while($row) {
        print '<tr>';
        foreach($row as $value) {
            print "<td>$value</td>";
        }
        print '</tr>';
        $row = mysql_fetch_assoc($result);
    }
    print '</table>';    
} else {
    echo "No Items Assigned";
};

It works for when the url is test.php?MobileNumber=... but not when its has TeamGroup or nothing in the url. The url I have is like this: test.php?TeamGroup=11

EDIT: Added more code to let you see what I am trying to display

if($_REQUEST=['MobileNumber']){ its a wrong syntax

Try this

add this line in your url ?MobileNumber=Put your mobile num here. Thanks

than for Team group again this ?TeamGroup=put team group here.

if($_GET['MobileNumber']==TRUE){
$MobileNumber=$_GET['MobileNumber'];
$Item = "SELECT a,b,c,d FROM Item WHERE MobileNumber = $MobileNumber";
}
elseif($_GET['TeamGroup']==TRUE){
$TeamGroup=$_GET['TeamGroup'];
$Item = "SELECT a,b,c,d FROM Item WHERE TeamGroup = $TeamGroup";
}
else{
    $Item = "SELECT a,b,c,d FROM Item";
    }

If you want to compare something using if condition then it must be like this

if(something == true){
  // then do something
}
else{
  //do something
}

Check the manual for more

What you are trying to do is not a valid one in php.You need to change this

if($_REQUEST=['MobileNumber']){

I think you can try like this

if(isset($_GET['MobileNumber']) && trim($_GET['MobileNumber'])){

And

elseif(isset($_GET['TeamGroup']) && trim($_GET['TeamGroup'])){

this should work...

if(isset($_GET['MobileNumber']))  //can also use if(isset($_GET['MobileNumber']) && !empty($_GET['MobileNumber']))
{
    $Item = "SELECT a,b,c,d FROM Item WHERE MobileNumber = ".$MobileNumber;
} 
elseif(isset($_GET['TeamGroup']))
{
    $Item = "SELECT a,b,c,d FROM Item WHERE TeamGroup = ".$TeamGroup";
}
else{
    $Item = "SELECT a,b,c,d FROM Item";
}

This statement:

if($_REQUEST=['MobileNumber'])

will always be true, because you're assigning the ['MobileNumber'] array to the $_REQUEST global.

If you're trying to check whether the values are set in the $_REQUEST global, do the following:

if( isset($_REQUEST['MobileNumber']))

You could try this:

$MobileNumber = $_GET["MobileNumber"];
$TeamGroup    = $_GET["TeamGroup"];

if(isset($_GET["MobileNumber"])) {
    //If you enclose fields/table in `` and values in '' you won't run into quoting issues
    //It's OK to not quote numbers, but you can quote them to stay standard
    $Item = "SELECT `a`, `b`, `c`, `d` FROM `Item` WHERE `MobileNumber` = '$MobileNumber'";
};
if(isset($_GET["TeamGroup"])) {
    $Item = "SELECT `a`, `b`, `c`, `d` FROM `Item` WHERE `TeamGroup` = '$TeamGroup'";
};
if(!isset($_GET["MobileNumber"]) && !isset($_GET["TeamGroup"])) {
    $Item = "SELECT `a`, `b`, `c`, `d` FROM `Item`";
};

//Switch to mysqli_*
$result = mysqli_query($dbLink, $Item) or die ("Error in query: $Item. " . mysqli_error());

if (mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    print '<table><tr>';
    foreach($row as $name => $value) {
        print "<th>$name</th>";
    };
    print '</tr>';

    while($row) {
        print '<tr>';
        foreach($row as $value) {
            print "<td>$value</td>";
        };
        print '</tr>';
        $row = mysqli_fetch_assoc($result);
    };
    print '</table>';    
} else {
    echo "No Items Assigned";
};

That

  • moves from mysql_* to mysqli_* (check my syntax - most of the mysqli_* functions are the same but there are a few differences)
  • uses isset & !isset to determine which query to use
  • encloses your query string in ticks and quotes to help make sure you don't forget to quote something properly
  • fixes your or die catch (you were referencing $query which didn't seem to exist)