dqbn76906 2018-07-13 09:38
浏览 27
已采纳

ORDER BY子句无法正常在Live服务器中工作

I have developed a php project which contains the table 'test'. The rows are fetched and shown and it is sorted by date column using ORDER BY clause in SQLquery. The following is the block of code used

<?php 
      foreach($stmt =$conn->query('SELECT * FROM test JOIN users USING(rollno)
      WHERE rollno='.$userRow['rollno'].' order by date desc')as $row){ ?>
        <button class="accordion"><b>Test on <?php echo date('d-m-Y', strtotime( $row['date'] )); ?></b></button>
          <div class="panel">
           <p>
           <table border="1">
            <?php
            echo "<tr><td>Date: " . date('d-m-Y', strtotime( $row['date'] )) . "</td></tr>";
            echo "<tr><td>Subject: " . $row['sub'] . "</td></tr>";
            echo "<tr><td>Topic: " . $row['topic'] . "</td></tr>";
            echo "<tr><td>Type: " . $row['type'] . "</td></tr>";
            echo "<tr><td>Total Marks: " . $row['totl_marks'] . "</td></tr>";
            echo "<tr><td>Marks Obtained: " . $row['marks'] . "</td></tr>";
            echo "<tr><td>Rank: " . $row['rank'] . "</td></tr>"; ?>
         </table>
            </p>
          </div>
      <?php }?> 

I want to sort the records such that the newest comes at top. The date stored in the database is in d-m-Y format. The system works fine at localhost, but when the project is made live, the records are not sorted as expected. The date column in database uses varchar type. What can be the problem?

  • 写回答

1条回答 默认 最新

  • dtn43447 2018-07-13 09:42
    关注

    STR_TO_DATE function in mysql can make the help. It will first convert date string in database to date type and then apply order by.

    Order by query should be:

    ORDER BY STR_TO_DATE(date,'%d-%m-%Y') DESC
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 问一下,定向解包是什么意思
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!