phpcodeigniterselectsubqueryresultset

How to access the value from a subquery in a SELECT clause in CodeIgniter


Not sure where I'm going wrong with my query, but the value dateread is being ignored and am getting an

error Message: Undefined property: stdClass::$dateread.

SELECT `id`.`id`, `id`.`name`, `id`.`deleted`, `id`.`filepath`, `id`.`createddate`, (SELECT createddate as dateread FROM documents_users iu WHERE id.id = iu.docs_id AND iu.users_id = 2346)
FROM (`documents` AS id)
JOIN `documents_years` AS iy ON `id`.`id` = `iy`.`docs_id`

Can anyone help?


Solution

  • Just update your query as and then fetch the value of $dateread you need to define dateread after completion of your sub_query and not within sub_query else it'll be as (SELECT createddate FROM documents_users iu WHERE id.id = iu.docs_id AND iu.users_id = 2346)

    SELECT `id`.`id`, `id`.`name`, `id`.`deleted`, `id`.`filepath`, `id`.`createddate`,
    (SELECT createddate FROM documents_users iu WHERE id.id = iu.docs_id AND iu.users_id = 2346) as dateread
    FROM (`documents` AS id)
    JOIN `documents_years` AS iy ON `id`.`id` = `iy`.`docs_id`