I am trying to return a list of users' first and last names in JSON format in PHP.
My PHP looks something like:
$_Query = '
SELECT
Fname,Lname
FROM
users
WHERE
number = "'.$_REQUEST['number'].'"
;';
$SQLResult = mysql_query($_Query) or die(mysql_error());
$_UserData = array();
if(mysql_num_rows($SQLResult) <> 0){
while($SQLRow = mysql_fetch_array($SQLResult)){
$_UserData['Fname'] = $SQLRow['Fname'];
$_UserData['Lname'] = $SQLRow['Lname'];
}
}
echo json_encode($_UserData);
I expect more than one row in the format
{"Fname":["First_name1","First_name2"],"Lname":["Last_name1","Last_name2"]}
of course. However the script returns the last row with the correct conditions
{"Fname":"Steve","Lname":"LastName"}
The MySQl server returns what it should with the same query.
+--------------+-------------+
| Fname | Lname |
+--------------+-------------+
| First_name1 | Last_name1 |
| First_name2 | Last_name2 |
+--------------+-------------+
Why is this happening and how should I go about fixing it? Thanks!
Try this:
while($SQLRow = mysql_fetch_array($SQLResult)){
$_UserData[] = $SQLRow;
}
In your example you are essentially overwriting the $_UserData array for every new row, hence the missing rows.
UPDATE : This will make the JSON string look more the way you want it.
while($SQLRow = mysql_fetch_assoc($SQLResult)){
$_UserData['Fname'][] = $SQLRow['Fname'];
$_UserData['Lname'][] = $SQLRow['Lname'];
}
$_UserData['Fname'] = $SQLRow['Fname'];
$_UserData['Lname'] = $SQLRow['Lname'];
You're repeatedly setting the same two elements of _UserData, rather than appending new values to the array.
Your loop overrides the values in $_UserData array. You should append each row to the array.
In addition, please note that you should escape your input.
Your code should look like this:
$_Query = '
SELECT
Fname,Lname
FROM
users
WHERE
number = "'.(int)$_REQUEST['number'].'"
;';
$SQLResult = mysql_query($_Query) or die(mysql_error());
$_UserData = array();
if(mysql_num_rows($SQLResult) <> 0){
while($SQLRow = mysql_fetch_array($SQLResult)){
$_UserData[] = $SQLRow;
}
}
echo json_encode($_UserData);
Try with this:
$_UserData = array();
if(mysql_num_rows($SQLResult) <> 0){
$i = 0;
while($SQLRow = mysql_fetch_array($SQLResult)){
$_UserData[$i]['Fname'] = $SQLRow['Fname'];
$_UserData[$i]['Lname'] = $SQLRow['Lname'];
$i++;
}
I'm sure you'll kick yourself when you realise that you've only assigned to 1 element of your $_UserData array.
Should be something like:
$_UserData = array();
if(mysql_num_rows($SQLResult) <> 0){
while($SQLRow = mysql_fetch_array($SQLResult)){
// Either...
// Create a new entry in $_UserData with same structure and contents as $SQLRow
$_UserData[] = $SQLRow;
// OR more specific - say if the structures aren't identical
// $DataRow=array();
// $DataRow['Fname'] = $SQLRow['Fname'];
// $DataRow['Lname'] = $SQLRow['Lname'];
// $_UserData[] = $DataRow;
}
}
Cheers
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.