0

I have an sql request:

 $listUsers=$connection->prepare("SELECT u.id as userid From user u , account a where (a.id_parent=$id_account OR  a.id=$id_account) and u.id_account=a.id ");
            $listUsers->execute();
            $users=$listUsers->fetchColumn();

but fetchColumn return just one result, and I want a set of list user id to use it in the next request.

 $listPush=$connection->prepare("select id from Table_name where id_user in (?));
 $listPush->bindValue(1, $users);
            $listPush->execute();
            $idpushs=$listPush->fetchColumn();

but this return just one result. Any Idea to replace fetchColumn by other request or using doctrince.

1
  • fetchColumn returns a single column from the next row of a result set. Try fetchAll. Commented Nov 25, 2015 at 9:42

1 Answer 1

1

With your logic, you can simply fetch all your users and implode it.

$listUsers=$connection->prepare("SELECT u.id as userid From user u , account a where (a.id_parent=$id_account OR  a.id=$id_account) and u.id_account=a.id ");
$listUsers->execute();
$users=$listUsers->fetchAll();
$userIds = array();
foreach ($users as $user) {
   $userIds[] = $user['userid'];
}
$users = implode(',', $userIds); //that hurts me so hard to code like this with symfony :'(

Fetch colum will only return the column of the next row. So for your second piece of code, you can loop over results.

But, if you're are using a framework like symfony, you have many other ways to make it cleaner. Just check out the symfony doc to use repository relations and more generally doctrine

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

2 Comments

the problem that the result is an array like this: 'array (size=1) 0 => array (size=1) 'userid' => string '10' (length=2)'
Updated. Please consider the end of my answer :)

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.