add multiple data in xml file using PHP xmlwriter

后端 未结 2 508
借酒劲吻你
借酒劲吻你 2021-01-14 09:07

I need to add article for each month in xml file using PHP xmlwriter:

$sql = \"SELECT *,YEAR(FROM_UNIXTIME(timestamp)) AS YEAR, 
           


        
2条回答
  •  一向
    一向 (楼主)
    2021-01-14 09:33

    Your problem is in the query:

    $sql = "SELECT *,YEAR(FROM_UNIXTIME(timestamp)) AS YEAR, 
                    MONTH(FROM_UNIXTIME(timestamp)) AS MONTH 
             FROM ".NEWS_ARTICLES." GROUP BY YEAR, MONTH ORDER BY YEAR DESC, MONTH ";  
    

    Most databases other than MySQL would reject this query, because you're grouping on 2 fields whilst selecting many fields.
    The group by year, month will only display a single (random) row from a month and hide all the others.

    The solution is to drop the group by clause entirely and rewrite the query like so:

    $sql = "SELECT *,YEAR(FROM_UNIXTIME(timestamp)) AS YEAR, 
                    MONTH(FROM_UNIXTIME(timestamp)) AS MONTH 
             FROM ".NEWS_ARTICLES." ORDER BY YEAR DESC, MONTH ASC";
    

    Remark
    It's odd that you don't have a WHERE clause. Do you really want to select all articles every time?
    A better approach would be to limit the number of articles selected in the query by some filter.
    It's always faster to filter in the database rather than in php.

提交回复
热议问题