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

  • then start with **prepared statements with paraeters** to prevent **sql injection** see https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – nbk Aug 01 '20 at 18:18
  • have you created a instance of PDO or mysqli, then you can use this https://stackoverflow.com/questions/30354280/php-pdo-method-to-fetch-simple-list-of-matched-elements – bhucho Aug 01 '20 at 18:19

1 Answers1

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.

mail2bapi
  • 1,547
  • 1
  • 12
  • 18