doushishi2415 2015-11-21 06:48 采纳率: 0%
浏览 84
已采纳

从数据库表android获取行

I'm trying to return the array of rows where the username matches the username in the database table. The JSON result just gives me the last row.

example table:

james 14 u@aol.com

mah 12 j@aol.com

james 23 ra@yahoo.com

result gives me:
23 ra@yahoo.com

but i want both james rows not just the last one. so both 14 u@aol.com and 23 ra@yahoo.com thanks

My php code:

<?php 
    $username = $_POST["username"];

    $con = new mysqli("xx", "xx", "xx", "xx");


    // selects everything 
    $statement = mysqli_prepare($con, "SELECT * FROM `user` WHERE username = ?");
    mysqli_stmt_bind_param($statement, "s", $username);

    mysqli_stmt_execute($statement);
    // Store the result to use
    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement, $id, $username, $age, $email);

    // Get results returned and put in array
    $user = array();
    while (mysqli_stmt_fetch($statement)) {
        $user[age] = $age;
        $user[email] = $email;
    }

    // Send array back to phone
    print (json_encode($user));

    mysqli_stmt_close($statement);
    mysqli_close($con);

?>
  • 写回答

3条回答 默认 最新

  • dongzhong1929 2015-11-21 07:02
    关注

    You overwrite the array so try something like this

    while (mysqli_stmt_fetch($statement)) {
            $user[] = array( 'age'=>$age, 'email'=>$email);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan