使用 JSON 从 phpmyadmin 获取从 phpmyadmin 到 Java 的多行


Get multiple rows from phpmyadmin to Java using JSON

大家晚上好,直到几天前,我从未使用过PHP或JSON,并且我发现使用JSON从phpmyadmin到Java的多行都遇到麻烦。我已经编写了下面的代码,但这只得到了最后一行。

我看了这篇文章(如何使用php将mysql中的多行编码为json)并尝试了一下,但没有运气。

    <?php
    $user = 'root';
    $pass = '';
    $db = 'uopuser';
    $con=mysqli_connect('localhost', $user, $pass, $db) or die('Unable to connect');

    $statement = mysqli_prepare($con, 'SELECT * FROM society');
    mysqli_stmt_execute($statement);
    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement, $society_id, $name, $email, $description);
    $society = array();
    while(mysqli_stmt_fetch($statement)){
        $society['society_id'] = $society_id;
        $society['name'] = $name;
        $society['email'] = $email;
        $society['description'] = $description;
    }
    echo json_encode($society);
    mysqli_stmt_close($statement);
    mysqli_close($con);
?>

提前谢谢你!

只需要将我的回声放入 while 循环中

while(mysqli_stmt_fetch($statement)){
            $society['society_id'] = $society_id;
            $society['name'] = $name;
            $society['email'] = $email;
            $society['description'] = $description;
        }
        echo json_encode($society);

while(mysqli_stmt_fetch($statement)){
        $society['society_id'] = $society_id;
        $society['name'] = $name;
        $society['email'] = $email;
        $society['description'] = $description;
        echo json_encode($society);
    }