从mysql数据库加载特定的行数,也增加了加载更多的功能


Loading Specific number of rows from mysql database and also adding load more feature

我正在使用php从mysql数据库加载数据。但是我想限制加载到特定的行数(例如:10行),并添加加载更多的功能,以获得更多的行使用Jquery ajax。

提前感谢!

限制MySQL中的查询:(这将返回前10行)

select * from table_name limit 10 

那么在javascript中,你可能需要计算你执行了多少次已经查询了数据库,所以你的查询将类似于:

select * from table_name limit 10,20
// 20,30 40,50 etc etc etc
// for example say you pass in a count variable, so this would be 
// 3rd time you've queried
// $_POST = 3
$lower = ($_POST['count'] * 10) - 10;
$upper = $_POST['count'] * 10;
// would result in:
select * from table_name limit $lower, $upper // 20, 30

但是你也可以使用mysql的offset关键字

$offset = $_POST['count'] * 10;
select * from table_name limit 10 offset $offset

这基本上是从数据库的下一个10行,你从AJAX查询。