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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配