简体   繁体   English

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

[英]Fetching only last row from PHP MySql in Android

Developing quiz application using Android - PHP MySql(DataBase) It's fetching only last row from the database, I want to implement and to fetch data one by one on next click. 使用Android开发测验应用程序-PHP MySql(DataBase)它仅从数据库中获取最后一行,我要实现并在单击时逐个获取数据。

So it will work like quiz 所以它将像测验一样工作

here is the recent code 这是最近的代码

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());
        }   
    }   

PHP select.php code PHP select.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);
?>

Please suggest some solution and how to implement next & previous functionality 请提出一些解决方案以及如何实现下一个和上一个功能

Yes, that is because once you're having the flag object with, say first record in the table, you are not using it and just overwriting it with data in next record on the next execution of the while-loop. 是的,这是因为一旦在表中的第一条记录中使用了标志对象,就不会使用它,而在下一次执行while循环时,只使用下一条记录中的数据覆盖它即可。

Do something like: 做类似的事情:

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);

To implement click for one-by-one results, keep a single counter for clicks for Next and Previous buttons and when one of them is pressed, send the value to a similar script where you can modify the query like: 要实现一对一结果的点击,请为“ 下一个”和“ 上一个”按钮的点击保留一个计数器,并在按下其中一个按钮时,将值发送到类似的脚本中,您可以在其中修改查询,例如:

$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"];
}

Similarly do a script for previous. 同样,为上一个脚本。 Manage the counter well. 做好柜台管理。 Initially initialize it to 1. That is when Next is pressed increase the counter by 1 and decrease it by 1 when Previous is pressed. 最初将其初始化为1。即,按Next(下一个 )时,将计数器加1,而按Previous(上一个 )时,将其减1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM