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

报告相同问题?

悬赏问题

  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发