-1

I have written an API script in PHP which receives a single value for parameter 'id' and returns the corresponding entry in db.How can I receive an array of values for 'id' and return all corresponding entries?

if(@$_GET["id"]){
    @$id=$_GET['id'];

    $where="where id=".$id;
 }else{
    $id=0;
    $where="";
 }

PS I have only started working with PHP.

2

1 Answer 1

0

It is bad practice to suppress error using @, instead, check it's existence with isset() function. Like this

if(isset($_GET["id"])){
           $id = $_GET["id"];
}

or

$id = isset($_GET["id"])? $_GET["id"] : 0;

Now, if your id post variable as an array of values. You can collect them using foreach, like this

if(isset($_GET["id"])){
   foreach($_GET["id"] as $id){
       echo $id; // use it as you want.
}
    }

I am assuming your id post variable and is a single dimension array.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.