dongwuxie5112 2011-02-27 07:35
浏览 39
已采纳

链接到PHP MYSQL中的排序表列

<?php
$con = mysql_connect ("localhost", "user", "pass") 
  or die ('Error: ' . mysql_error());
mysql_select_db ("members");

if(isset($_GET['orderby'])){ 
$order = $_GET['orderby']; 
$result = "SELECT * FROM persons ORDER BY ".mysql_real_escape_string($order)." DESC"; 
}
else{
$result = mysql_query("select * from persons");
}

$num_rows = mysql_num_rows($result);
$row_counter = 0; 

echo "<table width=600 border=0 cellspacing=0>
";
echo "<tr>

      <th>&nbsp;</th>

      <th>First Name</th>

      <th>Last Name</th>

      <th>Email Address</th>

      <th>City</th>

      <th>State</th>

      <th><a href='index.php?orderby=submitDate'>Date</a></th>

      </tr>";

while($row = mysql_fetch_array($result)){
  if($row_counter % 2){
    $row_color="bgcolor='#FFFFFF'";
  }
  else{
    $row_color="bgcolor='#F3F6F8'";
  }
  echo "<tr ".$row_color.">";
  echo "<td class='id'>" . $row['id'] . "</td>
";
  echo "<td>" . $row['firstName'] . "</td>
";
  echo "<td>" . $row['lastName'] . "</td>
";
  echo "<td>" . $row['email'] . "</td>
";
  echo "<td>" . $row['city'] . "</td>
";
  echo "<td>" . $row['state'] . "</td>
";
  echo "<td>" . $row['submitDate'] . "</td>
";
  echo "</tr>";
  $row_counter++;
  }
echo "</table>";

mysql_close($con);
?

>

I just cannot figure out why my link to sort my query is not working. any ideas? Pre-Thanks!

  • 写回答

1条回答 默认 最新

  • 普通网友 2011-02-27 07:43
    关注

    Your if else block was wrong. In one case $result is a query in another it is a result-set. Check code below.. fixed.

    <?php
    $con = mysql_connect ("localhost", "user", "pass") 
      or die ('Error: ' . mysql_error());
    mysql_select_db ("members");
    
    if(isset($_GET['orderby'])){ 
    $order = $_GET['orderby']; 
    $sql = "SELECT * FROM persons ORDER BY ".mysql_real_escape_string($order)." DESC"; 
    }
    else{
    $sql = "select * from persons";
    }
    $result = mysql_query($sql);
    $num_rows = mysql_num_rows($result);
    $row_counter = 0; 
    
    echo "<table width=600 border=0 cellspacing=0>
    ";
    echo "<tr>
    
          <th>&nbsp;</th>
    
          <th>First Name</th>
    
          <th>Last Name</th>
    
          <th>Email Address</th>
    
          <th>City</th>
    
          <th>State</th>
    
          <th><a href='index.php?orderby=submitDate'>Date</a></th>
    
          </tr>";
    
    while($row = mysql_fetch_array($result)){
      if($row_counter % 2){
        $row_color="bgcolor='#FFFFFF'";
      }
      else{
        $row_color="bgcolor='#F3F6F8'";
      }
      echo "<tr ".$row_color.">";
      echo "<td class='id'>" . $row['id'] . "</td>
    ";
      echo "<td>" . $row['firstName'] . "</td>
    ";
      echo "<td>" . $row['lastName'] . "</td>
    ";
      echo "<td>" . $row['email'] . "</td>
    ";
      echo "<td>" . $row['city'] . "</td>
    ";
      echo "<td>" . $row['state'] . "</td>
    ";
      echo "<td>" . $row['submitDate'] . "</td>
    ";
      echo "</tr>";
      $row_counter++;
      }
    echo "</table>";
    
    mysql_close($con);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来