Php 如何加上「;年龄“;要通过单击列标题对列进行排序?

Php 如何加上「;年龄“;要通过单击列标题对列进行排序?,php,mysql,Php,Mysql,我有一个添加行“age”,通过单击列名对表中的数据进行排序。我为此尝试了很多解决方案,但都不管用。我需要新的专栏“年龄”: 来自成员的TIMESTAMPDIFF(YEAR,CURDATE(),geburtstag) <?php // first creating database connection $servername = "localhost"; $username = "user"; $password = "password"; $database = "celebrates"

我有一个添加行“age”,通过单击列名对表中的数据进行排序。我为此尝试了很多解决方案,但都不管用。我需要新的专栏“年龄”: 来自成员的TIMESTAMPDIFF(YEAR,CURDATE(),geburtstag)

<?php
// first creating database connection
$servername = "localhost";
$username = "user";
$password = "password";
$database = "celebrates"; //this will contain name of a database
// Create connection
$conn = new mysqli($servername, $username, $password, $database);

mysqli_set_charset($conn, "utf8");
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$action = '';
$sql = "SELECT * from members";
$where = '';
if(isset($_GET["id"]))
{
    $id = $_GET["id"]; //geting id value which we are passing from table headers
    $action = $_GET["action"]; // geting action value which we are passing from table headers

    //we are checking condition if $action value is ASC then $action will set to DESC otherwise it will remain ASC
    if($action == 'ASC')
    {
        $action='DESC';
    } else {
        $action='ASC';
    }

    if($_GET['id']=='id') {
        $id = "id";
    } elseif($_GET['id']=='name') {
        $id = "name";
    } elseif($_GET['id']=='vorname') {
        $id="vorname";
    } elseif($_GET['id']=='geburtstag') {
        $id="geburtstag";
    } elseif($_GET['id']=='age') {
        $id = "age";
    } elseif($_GET['id']=='ort') {
        $id = "ort";
    }
    $where = " ORDER BY $id $action ";
    $sql = "SELECT * FROM members " . $where;
}
?>



<!DOCTYPE html>
<html lang="de">
<table>
  <tr>
    <th><a href="site.php?id=<?php echo 'name';?>&action=<?php echo $action;?>">NAME</a></th>
    <th><a href="site.php?id=<?php echo 'vorname';?>&action=<?php echo $action;?>">VORNAME</a></th>
    <th><a href="site.php?id=<?php echo 'geburtstag';?>&action=<?php echo $action;?>">GEBURTSTAG</a></th>
    <th><a href="site.php?id=<?php echo 'ort';?>&action=<?php echo $action;?>">ORT</a></th>
    <th><a href="site.php?id=<?php echo 'age';?>&action=<?php echo $action;?>">AGE</a></th>
 </tr>

<?php
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Fetch a result row as an associative array
    while($row = $result->fetch_assoc()) {
?>

  <tr>
   <td><?php echo $row["name"];?></td>
   <td><?php echo $row["vorname"];?></td>
   <td><?php echo date("j. F Y", strtotime($row["geburtstag"]));?></td>
   <td><?php echo $row['ort'];?></td>
   <td><?php echo $row['age'];?></td>
 </tr>

<?php
    }
    echo '</table>';
    echo '</div>';
} else {
    echo "0 results";
}

$conn->close();


您的
members
表中可能没有名为
age
的列。只需稍微增强您的查询:

$sql = "SELECT members.*, TIMESTAMPDIFF(YEAR,CURDATE(),geburtstag) as age FROM members " . $where

您不能计算年龄减去年,因为年龄的计算也考虑了天数

使用此查询:

    SELECT *, 
           YEAR( CURRENT_TIMESTAMP ) - YEAR( geburtstag ) - (SUBSTR(CURRENT_TIMESTAMP,6,5 ) < SUBSTR(geburtstag,6, 5)) as age
      FROM members
选择*,
年(当前时间戳)-年(geburtstag)-(SUBSTR(当前时间戳,6,5)


通过此查询,如果当前月/日(
SUBSTR(当前时间戳,6,5)
)小于生日月/日(
(SUBSTR(geburtstag,6,5)

),则在添加
打印($row)后,从年差中减去1
并发布结果谢谢,但不起作用…name、vorname、geburtstag、ort来自SQL表,我想做临时的“age”…如何和在哪里?Thanksage不在表中,因此存在问题P.S........$id=$之后的所有$id if/elseif语句\u GET[“id”];实际上是冗余的。首先将$id var设置为get请求的值,然后检查get请求的值是什么,如果是'id','name'等,则再次将$id var设置为该值。这没有意义。我是PHP新手,同样,我不会说英语。我是德国人。:)你能给我写一个完整的代码吗?id=id(主键)name=varchar vorname=varchar geburtstag=date ort=varchar谢谢。注意:未定义索引:第行中的年龄:“php echo$row['age']”谢谢,但不起作用。你能帮助我吗?确切地说是哪一行。我是PHP新手,同样,我不会说英语。我是德国人,然后呢?它在日期上可以正常工作。你试过了吗?它对你无效?我试过了,它对我有效。事实上,您也可以使用
TIMESTAMPDIFF
,但必须反转参数:
TIMESTAMPDIFF(年份、geburtstag、当前日期)
只需替换
$sql=“SELECT*from members”与上面的脚本。为什么不起作用?加倍检查!另请参见此日期类型:它有效!我试过了,但没用$其中=“按$id$action订购”;`$sql=“选择members.*,TIMESTAMPDIFF(YEAR,geburtstag,CURDATE())作为成员的年龄”;`$其中=“”;`这已经显示了年龄,但不按标题上的单击进行排序No a have not column“age”$where=“ORDER by$id$action”$sql=“选择members.*,TIMESTAMPDIFF(YEAR,geburtstag,CURDATE())作为成员的年龄”$其中=“”;这已经显示了年龄,但不按点击标题排序谢谢:)谢谢。这是确定的:)
$sql=“选择成员。*,TIMESTAMPDIFF(YEAR,geburtstag,CURDATE())作为成员的年龄”$在哪里