I know there are too many related questions to the title, but so many answers out there didn't help me achieve what i'm trying to do.

I am trying to execute the following code example on mysql:

SELECT * FROM users 
WHERE user_id in (1, 2, 3, 4)

The ids are dynamic and it is passed from flutter to PHP Api. So in flutter, my list is like this:

listOfIds = [1, 2, 3, 4]; //This is dynamic
stringIds = listOfIds.join(","); //Output will be "1,2,3,4"

I then pass it to my PHP APi using the following code:

var response = await http.post(
      Uri.parse('example'),
      headers: {'Accept': 'application/json'},
      body: {
        'ids': stringIds, //has to be converted to string
      },
    );

And my php api is like this:

$ids = $_POST['ids'];

$sql = "SELECT * FROM users WHERE user_id in ('".$ids."') ";

So now, the issue here is that the query is doing

where user_id in ("1, 2, 3, 4")

All I need to do is remove the quotes which is converting from string to int.

I tried the following:

$intIds = implode(',', $ids);

$intIds = array_map('interval', explode(',', $ids));

$intIds = array_map('interval', json_decode($ids, true));

and everything in Convert a comma-delimited string into array of integers?

But nothing is working for the sql query i am trying to execute

0

There are 0 best solutions below