MYSQL Select from table, get newest/last 10 rows in table

后端 未结 4 1685
礼貌的吻别
礼貌的吻别 2021-02-04 09:50

What\'s the best, and easiest way to do this? My query currently is:

  SELECT * 
    FROM chat 
   WHERE (userID = $session AND toID = $friendID) 
      OR (use         


        
4条回答
  •  遇见更好的自我
    2021-02-04 10:27

    $con = mysqli_connect("localhost","my_user","my_password","my_db");
    $limit = 10;                
    $query = "SELECT * FROM  $table";
    $resource = mysqli_query($con,$query);
    $total_rows = mysqli_num_rows($resource);
    $start = $total_rows-$limit;
    $query_limit= $query." LIMIT $start,$limit";
    

    First I have set the limit

    $limit = 10;
    

    then

     $total_rows = mysqli_num_rows($resource);
    

    Here I have taken total number of rows affected.

    $start = $total_rows-$limit;
    

    then substracted limit from number of rows to take starting record number

       $query_limit= $query." LIMIT $start,$limit";
    

    and then added limit to the query. For more information about limit see this link https://www.w3schools.com/php/php_mysql_select_limit.asp

提交回复
热议问题