dousi4257 2012-03-12 18:41
浏览 28
已采纳

PHP / MySQL其中/ order子句没有排序数据

I am trying to order data from the table 'tech_inquiry' by the Field 'number' in descending order. This should put the results in order by year. Each 'number' field corresponds to another field with a title/date (what is actually viewed by visitors) which I can't sort by because the date is at the end of the title and not always in the same place.

Also, the table 'tech_inquiry_allowed' determines what is viewable to who when logged in.

With that said, here is the code:

<?
        $query2=mysql_query("SELECT * FROM tech_inquiry_allowed where term_code = '$term_code' ");
        while($row2=mysql_fetch_assoc($query2))
            {
                $id2=$row2['id'];
                $query3=mysql_query("SELECT * FROM tech_inquiry WHERE id= '$id2' ORDER BY number DESC");
                $row3=mysql_fetch_assoc($query3);
                $name3=$row3['name'];
                    ?> 
                        <hr />
                        <li><a href="get_file.php?id=<? echo $id2; ?> "><? echo $name3; ?> </a> </li> 
                    <?   
            }
    ?>

Also, I have another 'admin' section that is able to order data correctly. The only difference is there is no conditional 'where' clause because no user authentication is needed (it is used to add data to the table).

Here is that code:

<? 
$query2= mysql_query("SELECT * from tech_inquiry ORDER BY number DESC");
while($row2=mysql_fetch_assoc($query2))
{
    $id2=$row2['id'];
    $name2=$row2['name'];
    ?> 
    <hr />
    <li><a href="get_file.php?id=<? echo $id2; ?> "><? echo $name2; ?> </a> </li> 

    <?

I am wondering if it might be the fact that we are running a query inside a loop.

  • 写回答

2条回答 默认 最新

  • doushi3819244 2012-03-12 18:53
    关注

    There are a number of problems here. First of all you only need one query to accomplish this. Please read up on SQL query writing when you get a moment. You will find it to be VERY helpful.

    Second, you are using way more code than you need to. Below is the much simplified, cleaner, and probably faster code.

    <?php
    $query = mysql_query("SELECT * FROM tech_inquiry ti WHERE id IN (SELECT id FROM tech_inquiry_allowed where term_code = '$term_code') ORDER BY ti.number");
    
    while ($row = mysql_fetch_object($query)) {
      echo "<hr />
    <li><a href='get_file.php?id={$row->id}'>{$row->name}</a></li>";
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler