Abs*_*Abs 2 php mysql recordset
我有以下PHP函数来确定数据库中的下一行和上一行.但是,有很多情况下可以删除行,因此我的函数不能正常工作,因为它们只会减少auto_increment字段.
例如,当前行5.我的函数给出:4(上一个)和下一个(下一个).如果删除6和7怎么办?我最好的想法是继续查询,直到我排好,但这似乎效率低下,有更好的方法吗?
谢谢大家
//function to get next tweet
function getNextTweet($key, $direction){
$sql = "SELECT tweet_id FROM tweets WHERE tweet_key = '$key' LIMIT 1";
$result = mysql_query($sql) or die("DB Error : ". mysql_error());
$result = mysql_fetch_assoc($result);
if($direction=='next'){
$tweet_id = $result['tweet_id'] + 1;
}else{
$tweet_id = $result['tweet_id'] - 1;
}
$sql = "SELECT * FROM tweets WHERE tweet_id = '$tweet_id' LIMIT 1";
$result = mysql_query($sql) or die("DB Error : ". mysql_error());
return mysql_fetch_assoc($result);
}
Run Code Online (Sandbox Code Playgroud)
假设你没有bajillion记录......
以前:
SELECT *
FROM table
WHERE (id < currentID)
ORDER BY id DESC
LIMIT 1
Run Code Online (Sandbox Code Playgroud)
下一个
SELECT *
FROM table
WHERE (id > currentID)
ORDER BY id ASC
LIMIT 1
Run Code Online (Sandbox Code Playgroud)