php更新查询和订单

时间:2017-04-26 08:01:18

标签: php mysql

请问我在更新后遇到有关记录顺序的问题。 我有两页home.phpsettings.php。  home.php包含来自mysli选择查询的结果。

$getid = $getdata['user_id'];
$query = mysqli_query($con,"SELECT * FROM  posts 
                            WHERE `user_id` = ' $get_id' 
                            ORDER BY date_added DESC 
                            LIMIT 15 ");

所选记录为id username notes date_added,这些记录以desc顺序成功显示。

在我的settings.php我允许用户更新用户名,同时更新用户名时,我还会运行更新查询,更改notes表上的用户名。在user表和notes表上都成功更新了新用户名。但不幸的是,当我回到home.php时,笔记不会再次以desc的顺序显示。它正在显示表格顶部的注释。

更新查询功能(从settings.php调用):

function update_user($update_data){
    $con = db();
    global $session_user_id;
    $update= array();
    array_walk($update_data, 'array_sanitize');
    foreach ($update_data as $field => $data) {
        $update[] = '`'. $field . '` =\''.$data .'\'';
    }
    mysqli_query($con,"UPDATE `users` SET " 
                       . implode(', ', $update) . , 
                      " WHERE `user_id` = '$session_user_id'");

    mysqli_query($con,"UPDATE notes SET username = 
                                (SELECT `username`
                                FROM  users 
                                WHERE user_id = $session_user_id
                                ) 
                        WHERE user_id = $session_user_id ");
}

我尝试按date_added排序,但它还没有正常工作。 请帮助我,谢谢。

1 个答案:

答案 0 :(得分:0)

you have to change date format to UNIX_TIMESTAMP to apply order by in 
query 
$query = mysqli_query($con,"SELECT * ,UNIX_TIMESTAMP(date_added) as date FROM  posts 
                        WHERE `user_id` = ' $get_id' 
                        ORDER BY date DESC 
                        LIMIT 15 ");