0

I am trying to get data from two tables names as

1.TICKET_MESSAGE_TABLE

2.TICKET_ATTACHMENT_TABLE

Query is below

$sql='SELECT msg.*, count(attach_id) as attachments  
      FROM '.TICKET_MESSAGE_TABLE.' msg '.
        ' LEFT JOIN '.TICKET_ATTACHMENT_TABLE.' attach ON  msg.ticket_id=attach.ticket_id 
                    AND msg.msg_id=attach.ref_id '.
    ' WHERE  msg.ticket_id='.db_input($this->getId()).
    ' GROUP BY msg.msg_id 
      ORDER BY msg.created desc';

$msgres=db_query($sql);
      
return $msgres;

The problem is the last record from the table TICKET_MESSAGE_TABLE is not fetching until we not insert the new record in this table. Can any one have idea of this problem. Please.

Community
  • 1
  • 1
Rana Aalamgeer
  • 702
  • 2
  • 8
  • 22
  • 1
    What does `db_query()` do. Likely the error is in there – RiggsFolly Sep 26 '16 at 12:16
  • When i do ORDER BY msg.created asc it work fine. But not working with desc. – Rana Aalamgeer Sep 26 '16 at 12:17
  • db_query is like mysql_query() its my function. – Rana Aalamgeer Sep 26 '16 at 12:17
  • 1
    How are you processing the result? – Pipe Sep 26 '16 at 12:20
  • suppose you have 10 rows if you do ORDER BY msg.created asc you will get all 10 rows but with desc you will get 9 rows??? is this what happening? – Archish Sep 26 '16 at 12:20
  • 3
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Sep 26 '16 at 12:21
  • Are you sure you are not just looking at your data incorrectly. I dont believe if all you change is the sort order it will loose a row. Start by **counting** the rows – RiggsFolly Sep 26 '16 at 12:21
  • @Archish yes this is the problem. Have you any idea?? – Rana Aalamgeer Sep 26 '16 at 12:25
  • Does it works same in console? – Arnial Sep 26 '16 at 13:06
  • It is highly unlikely that this issue is a result of the sql query, this would be a pretty big bug in mysql – Shadow Sep 26 '16 at 13:19
  • If so then error in methods that receive or process the result, which are unknown to us. – Arnial Sep 26 '16 at 13:34

0 Answers0