0

I am using the following code for passing the array value into mysql query but array to string conversion error will come

$sql =mysql_query("SELECT userId from groupmembers where groupId='$groupId'");
$bjson = array();
$i = 0; 
while($result=mysql_fetch_assoc($sql))
{ 
    $bjson[$i]['userId'] = $result['userId'];

    $i++; 
}

$query = "SELECT firstName 
     FROM users 
     WHERE userId IN('" . implode("','", $bjson) ."')";
4

2 Answers 2

6

No need of two query and loop just use

SELECT firstName 
     FROM users 
     WHERE userId IN(SELECT userId from groupmembers where groupId='$groupId')

Note:- mysql is deprecated instead use mysqli or PDO

Read How can I prevent SQL injection in PHP?

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

Comments

1

Can use a join in your query to save the loop

SELECT u.firstName FROM users u JOIN groupmembers gm ON gm.userID = u.userID WHERE gm.groupID='$groupId' 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.