如何在sql中选择特定范围内的行?

2cmtqfgy  于 2021-08-09  发布在  Java
关注(0)|答案(2)|浏览(333)

我正在创建一个简单的消息传递web应用程序。人们可以通过登录轻松地发送或接收短信。只需点击特定的联系人姓名,您将拥有一个包含所有先前消息的聊天框。现在的问题是,任何包含大量消息的会话都需要很长时间才能加载聊天室。所以我想第一次在聊天框中显示最新的(orderby sendingtime desc)10条消息。在聊天框的顶部,我想提供一个按钮,通过点击哪个用户将获得前10条消息(order by sendingtime desc),它将一直持续到用户获得最后一条消息。我使用的是php和ajax。我用来获取前10条信息的代码在这里

$sql2="select * from messages where sender='$user1' and reciever='$user2' or reciever='$user1' and 
sender='$user2' order by sendingTime desc";
$res2=mysqli_query($con,$sql2);
$num=mysqli_num_rows($res2);
if($num<=10){
for($i=1;$i<=$num;$i++){
$row2=mysqli_fetch_array($res2);
$data2[]=$row2;
}
}else{
for($i=1;$i<=10;$i++){
$row2=mysqli_fetch_array($res2);
$data2[]=$row2;
}
}

但我不知道如何选择接下来的10个消息行。请帮忙。
我的“消息”数据库结构1 idprimary int(11)no none自动增量更改drop
更多2个发送者varchar(100)拉丁文1\u瑞典语\u ci无更改拖放
更多更多3个接收者varchar(100)拉丁文1\u瑞典语\u ci无更改拖放
更多4 msg varchar(5000)拉丁语1\u瑞典语\u ci无更改拖放
更多更多5 sendingtime varchar(100)拉丁文1\u瑞典语\u ci无更改拖放
更多6交货时间varchar(100)拉丁文1\u瑞典语\u ci无更改拖放
更多7状态int(1)否

hgqdbh6s

hgqdbh6s1#

警告!:您目前对sql注入(sqli攻击)非常开放。请使用准备好的语句和参数化查询,使用mysqli或pdo。你的数据库有很大的风险!不要相信任何用户输入!
话虽如此,我将继续使用mysqli准备的语句和参数化查询来回答这个问题。
您在查询中通常需要的是 LIMIT 接线员。有关 LIMIT 接线员可以在这里找到。
例子:

SELECT
    *
FROM
    messages
WHERE
    sender = 1 AND receiver = 2
    OR ( receiver = 1 AND sender = 2)
LIMIT 10;

我在这儿拉小提琴。
在ajax函数中,需要解析一个值,该值将作为 LIMIT 条款。每次单击“加载更多”,该值就会增加10。
以下是您的php页面的外观:

<?php
// Your user variables
$user1 = $_POST['user1'];
$user2 = $_POST['user2'];

// Your amount of messages to be loaded
$loadMessages = $_POST['messageAmount'];

if(empty($loadMessages )) {
   $loadMessages = 0; 
}

// DB variables
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";

// Error handling
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

/*
Declare the query.
One thing to note from a select statement,
is that it is generally considered better practice
to list the fields you want to fetch.
Even if it's considered to be all fields.
So I would recommend doing that over "SELECT *".

* /

$sql = "SELECT
            *
        FROM
            messages
        WHERE
            sender = ?
            AND receiver = ? 
            or (reciever = ? and sender = ?)
        LIMIT (10 + ?)
        ORDER BY
            sendingTime DESC";

/*
Prepare and bind 
The variable identifiers are based on assuming the sender and receiver
are int fields. Change the identifier if that's not the case.

Identifiers:
-s string
-i int
-d double
-b BLOB

* /

$stmt = $conn->prepare($sql);
$stmt->bind_param('iiiii', $user1, $user2, $user1, $user2, $loadMessages);

// Execute the query
$stmt->execute();

// Get result of query
$result = $stmt->get_result();

// Close connection
$stmt->close();
$conn->close();
?>

要循环查看结果:

<?php
// Check if any returned rows?
if($result->num_rows > 0) {
    // Loop through the result(s)
    while ($data = $result->fetch_assoc()) {
        echo $data['field_name'];
    }
} else {
    // Something here?
}
?>
g6ll5ycj

g6ll5ycj2#

中间试验操作员
其中列名称介于value1和value2之间;
设置值1和值2的条件
检查语法

相关问题