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);
?>
您的数组应该为空,不像[age] [email]那样,它应该为空,以便数组创建索引。
You overwrite the array so try something like this
while (mysqli_stmt_fetch($statement)) {
$user[] = array( 'age'=>$age, 'email'=>$email);
}
user[]
should be a 2 dimensional array.
In your case last element will be there in the array.
while (mysqli_stmt_fetch($statement)) {
$user[] = array('age'=>$age,'email' =>$email);
}
Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.