0

I have a query which fetches distinct user ids and I am trying to convert it into a comma separated string which I can pass into another sql query having IN in WHERE clause. But I am getting an error saying array to string conversion.

$qry0="SELECT DISTINCT id FROM users  ";
$res0=getData($qry0);
while($row0=mysqli_fetch_array($res0))
{
    $data0      =   $row0['id'];  
}

And I'm trying to convert it as string like this:

$array = explode(",", $data0);

and pass it to another

$qry="SELECT * FROM login WHERE clientid IN(".$array.") ";
2
  • 1
    explode creates an array. where implode creates a string ... an SQL query requires a string, not an array. Commented May 19, 2016 at 13:46
  • $data0 is not an array.It is a variable with last value of id from your loop . Commented May 19, 2016 at 13:46

2 Answers 2

1

USe implode instead of explode:

$qry0="SELECT DISTINCT id FROM users  ";
$res0=getData($qry0);
$data0 = array(); // initialize array first
while($row0=mysqli_fetch_array($res0))
{
    $data0[]      =   $row0['id'];  // create array like this
}

$array = implode(",", $data0); // use implode to convert array to string
Sign up to request clarification or add additional context in comments.

Comments

0

The explode() function breaks a string into an array.To break array into string you need to use implode()

$qry0="SELECT DISTINCT id FROM users  ";
$res0=getData($qry0);
$data0 = array();
while($row0=mysqli_fetch_array($res0))
{
    $data0[]      =   $row0['id'];  
}
$array = implode(",", $data0);

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.