仅从 PHP MySql 在 Android 中获取最后一行

Fetching only last row from PHP MySql in Android

使用 Android - PHP MySql(数据库)开发测验应用程序 它只从数据库中获取最后一行,我想实现并在下次点击时一个一个地获取数据。

所以它会像测验一样工作

这是最近的代码

protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        try
        {
            JSONObject json_data = new JSONObject(res); 

            quizID =(json_data.getString("quizID")); 
            question =(json_data.getString("question")); 
            choice1 =(json_data.getString("choice1")); 
            choice2 =(json_data.getString("choice2")); 
            choice3 =(json_data.getString("choice3")); 
            answer =(json_data.getString("answer")); 

            txtno.setText("No. " + quizID); 
            txtque.setText("" + question); 
            ropt0.setText("" + choice1); 
            ropt1.setText("" + choice2); 
            ropt2.setText("" + choice3); 
            ranswer.setText("" + answer);

        }
        catch(Exception e)
        {
            Log.e("Fail 3", e.toString());
        }   
    }   

PHPselect.php代码

<?php
$host='localhost';
$uname='root';
$pwd='';
$db="QuizDB";

$con = mysqli_connect($host,$uname,$pwd,$db) or die("connection failed");

$quizID=$_REQUEST['quizID'];
$question=$_REQUEST['question'];
$choice1=$_REQUEST['choice1'];
$choice2=$_REQUEST['choice2'];
$choice3=$_REQUEST['choice3'];
$answer=$_REQUEST['answer'];

$sql = "SELECT * FROM QuizQuestion"; 
$result = mysqli_query($con, $sql);

while($row=mysqli_fetch_array($result))
{
    $flag['quizID']=$row["quizID"];
    $flag['question']=$row["question"];
    $flag['choice1']=$row["choice1"];
    $flag['choice2']=$row["choice2"];
    $flag['choice3']=$row["choice3"];
    $flag['answer']=$row["answer"];
}

print(json_encode($flag));
mysqli_close($con);
?>

请提出一些解决方案以及如何实现下一个和上一个功能

是的,那是因为一旦你有了标志对象,比如 table 中的第一条记录,你就不会使用它,只是在下一次执行时用下一条记录中的数据覆盖它while 循环。

做类似的事情:

while($row=mysqli_fetch_array($result))
{
    $flag['quizID']=$row["quizID"];
    $flag['question']=$row["question"];
    $flag['choice1']=$row["choice1"];
    $flag['choice2']=$row["choice2"];
    $flag['choice3']=$row["choice3"];
    $flag['answer']=$row["answer"];
    ...send flag object to wherever you  want...
}
mysqli_close($con);

要实现一对一结果的点击,请为 NextPrevious 按钮保留一个单独的点击计数器,并且当一个其中一个被按下,将值发送到类似的脚本,您可以在其中修改查询,如:

$sql = "SELECT * FROM table ORDER BY quizID LIMIT "+counter+"-1,1"; 
$result = mysqli_query($con, $sql);

while($row=mysqli_fetch_array($result))
{
    $flag['quizID']=$row["quizID"];
    $flag['question']=$row["question"];
    $flag['choice1']=$row["choice1"];
    $flag['choice2']=$row["choice2"];
    $flag['choice3']=$row["choice3"];
    $flag['answer']=$row["answer"];
}

同样为previous做一个脚本。管理好柜台。最初将其初始化为 1。也就是说,当按下 Next 时,计数器加 1,按下 Previous 时,计数器减 1。