duanbu1998 2014-01-25 19:24 采纳率: 0%
浏览 32
已采纳

在类中加载数据库表

Is it a good practice to load an entire table from a database into a class?

for example a user class? Adding all the information of users in a class?

  • 写回答

1条回答 默认 最新

  • douaipi3965 2014-01-25 21:11
    关注

    If your table has a few data it will be fine to get all the information. e.g. if user table has only 3 users; student, tutor and admin along with their ids it is ok to get all the data.

    But if the table has a large amount of data it is not a good practice to load whole table. Therefore you can use the operations such as LIMIT to query limited number of rows.

    LIMIT example:

    SELECT * FROM `my_table` LIMIT 0, 5 
    

    (This will display the first 5 results from 'my_table'.)

    For pagination following will be useful:

    1. totalRows = the total number of rows in DB. (can be taken using COUNT in sql)
    2. itemsPerPage = define how many items you want to show in a single page
    3. numberOfPages = totalRows/ itemsPerPage (numberOfPages is useful to display the page numbers in the page).
    4. startingItem = (pageNumber - 1) * itemsPerPage
    5. SELECT * FROM my_table LIMIT startingItem, itemsPerPage

    So basically you will need to pass the pageNumber to a function (when user clicks on a page) in order to retrieve limited number of rows.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?