0
$query = $em->query("
    SELECT c.id AS id
    FROM collectif c, zone z
    WHERE
        c.zone_id = z.id
    AND z.label = '$zone'
    ANDc.collectif = '$collectif'
");

$c = $query->fetchAll();
$idc = $c['id'];

I have this query that returns a single line, Symfony shows me an error as what variable id undefined

NB: I know that's don't respect the concept of Symfony [MVC] but it's for a particular reason so if someone can tell me how I can resolve this problem

Thank you

4
  • 2
    $idc = $c[0]['id']; ? Commented Jul 26, 2017 at 20:32
  • 1
    Very curious what your "particular reason" is. It's probably your real problem for which you should be asking a question on how to resolve it. Commented Jul 26, 2017 at 20:36
  • It's not to show a bad example that I specify here that it's a particular reason, I used a request in a controller Commented Jul 26, 2017 at 21:17
  • 1
    z.label = '$zone' and c.collectif = '$collectif' - please don't do this. please create parameters and set them on your query...it will be a lot safer that way Commented Jul 26, 2017 at 21:29

2 Answers 2

2

$query->fetchAll() should return numeric array of elements so key id does not exists. You should try $c[0]['id'] to get value.

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

Comments

0

If you'd rather use the results in the assocative way, you can use fetchAssoc() instead:

$c = $query->fetchAssoc();
$idc = $c['id'];

Here is the documentation for reference: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/data-retrieval-and-manipulation.html#fetchassoc

I'm just giving an alternate way to do this.

3 Comments

Did this help at all?
No, it did not work, the solution is : $query->fetchAll();$c[0]['id'];
But as per your original post you wanted to use $c['id'], so in that case if you use fetchAssoc(0) instead, then you can use $c['id'] like I've indicated in my answer. I use fetchAssoc() exactly in this way, so I know it works. I was only trying to provide you with a better solution.

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.