undefined GET id?

2019-09-06 17:08发布

问题:

 <?php
$s = $_GET["s"];
if($s) {
$hent_b = mysql_query("SELECT * FROM member_battles WHERE state = '1' ORDER BY id DESC LIMIT 0,200") or die(mysql_error());
}else{
$hent_b = mysql_query("SELECT * FROM member_battles WHERE state = '0' ORDER BY id DESC LIMIT 0,200") or die(mysql_error());
}
while($vis = mysql_Fetch_array($hent_b)) {
 ?> 

I have this now i want when i enter my site (index.php) it should not come up undefined $_GET["s"];

how do i do this? but i want when you do index.php?s then it should change the query

回答1:

Use isset() or array_key_exists():

if(isset($_GET['s'])) {

}

or

if(array_key_exists('s', $_GET)) {

}

This first checks if there really is an element in the array with a key s.

Don't assign $_GET['s'] to a variable before, otherwise you will have the same issue.


I personally would always assign a value to a parameter, i.e. using index.php?s=1 instead of just index.php?s.



标签: php get