gpt4 book ai didi

php - 从数据库表android中获取行

转载 作者:太空宇宙 更新时间:2023-11-03 11:54:18 24 4
gpt4 key购买 nike

我正在尝试返回用户名与数据库表中的用户名匹配的行数组。 JSON 结果只给出了最后一行。

示例表:

james 14 u@aol.com

mah 12 j@aol.com

james 23 ra@yahoo.com

result gives me:
23 ra@yahoo.com

但我想要两个 james 行,而不仅仅是最后一个。所以 14 u@aol.com 和 23 ra@yahoo.com 谢谢

我的 php 代码:

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

?>

最佳答案

你覆盖了数组,所以尝试这样的事情

while (mysqli_stmt_fetch($statement)) {
$user[] = array( 'age'=>$age, 'email'=>$email);
}

关于php - 从数据库表android中获取行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33840567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com