如何从时间戳中选择年份并回显它

时间:2014-02-25 17:59:25

标签: php mysql

当我使用heidi尝试mysql时,我需要的值被发布但是当我尝试使用它时它不会回显任何东西

        $value = mysql_query("SELECT max(student_id) AS id FROM student");
        $qryResultArray = mysql_fetch_array($value);
        $student_id = $qryResultArray['id'];

        $result = mysql_query("SELECT DATE_FORMAT(date_enrolled, '%Y') AS yr,
        DATE_FORMAT(date_enrolled, '%m') AS mth
        FROM student_course WHERE id=$student_id");
        $row = mysql_fetch_array($result);
        $yr = $row['yr'];
        $mth = $row['mth'];

        //when i echo the statement nothing is echoed
        echo "year ".$yr."<br>";
        echo "mth ".$mth."<br>";

1 个答案:

答案 0 :(得分:1)

只需使用一个查询:

$result = mysql_query("SELECT YEAR(date_enrolled) AS yr,
    DATE_FORMAT(date_enrolled, '%m') AS mth
    FROM student_course WHERE id=(SELECT max(student_id) AS id FROM student)");

更新

    $value = mysql_query("SELECT max(student_id) AS id FROM student");
    $qryResultArray = mysql_fetch_array($value);
    $student_id = $qryResultArray['id'];

    $result = mysql_query("SELECT YEAR(date_enrolled) AS yr,
    MONTH(date_enrolled) AS mth
    FROM student_course WHERE id= ".$student_id."");
    $row = mysql_fetch_array($result);
    $yr = $row['yr'];
    $mth = $row['mth'];

    //when i echo the statement nothing is echoed
    echo "year ".$yr."<br>";
    echo "mth ".$mth."<br>";