I am fetching json data this
$sql=mysqli_query($con,"select * FROM mohamiusermeta where meta_key='websiteurl' or meta_key='profile_photo' or meta_key = 'Office' or meta_key='address_user' or meta_key='agentarea' or meta_key='offertext' or meta_key='officename' or meta_key='cover_photo' or meta_key='membertype' order by user_id desc");
$newarray = array();
$subArray = array();
while($row=mysqli_fetch_assoc($sql))
{
$output[$row['meta_key']] = $row['meta_value'];
$subArray[user_id]=$row['user_id'];
$subArray[Office]=$output['Office'];
$subArray[officename]=$output['officename'];
$subArray[address_user]=$output['address_user'];
$subArray[profile_photo]=$output['profile_photo'];
$subArray[cover_photo]=$output['cover_photo'];
$subArray[agentarea]=$output['agentarea'];
$subArray[offertext]=$output['offertext'];
$subArray[websiteurl]=$output['websiteurl'];
$subArray[membertype]=$output['membertype'];
$newarray[] = $subArray;
}
json_encode($newarray);
echo(json_encode($newarray,JSON_UNESCAPED_UNICODE));
mysqli_close();
?>
but the result in browser get null and duplicate value for the first row where it has a value in database
what is wrong in fetching array? to check result in browser visit http://lifecareclub.net/api/test.php
You are modifying what already is added to $newarray
in the next iteration, when you modify members of $subArray
.
To solve this, move $subArray = array();
inside the loop, just before the first assignment to one of its keys. That way you make a new array in each iteration, which "cuts it loose" from the data that you assigned to its members in the previous iteration.
NB: you need to use quotes in $subArray['user_id'] = ...
and other assignments, but I suppose that is a typo in your question.
I can see a few issues here with the way the query results are pulled from the database and converted to your array. In general for debugging problems like this, you should look at the values of your variables along the way, and see if they match expectations.
For instance, if you put
var_dump($output);
in your loop before you start using $output
to set values in $subArray
, you will see that it does not have all the information you think it does.
Side note: if you are using Ajax to call the function, you need to learn to use your browser's debugging tools to look at the exact data returned from the server. That way you can return non-JSON results and see what is really going on. In this case, you will see that the returned data was actually an error message that would help you figure out your next step.
I don't want to write your code for you, but here are a couple of things you can do to improve your code. I am assuming that things like officename
are columns in your database table that are returned by the SELECT
statement.
$sql=mysqli_query($con,"select * FROM mohamiusermeta where meta_key IN ('websiteurl', 'profile_photo', 'Office', 'address_user', 'agentarea', 'offertext' , 'officename', 'cover_photo', 'membertype') order by user_id desc");
$newarray = array();
while($row = mysqli_fetch_assoc($sql))
{
$subArray = array();
$subArray[$row['meta_key']] = $row['meta_value'];
$subArray['user_id'] = $row['user_id'];
$subArray['Office'] = $row['Office'];
$subArray['officename'] = $row['officename'];
$subArray['address_user'] = $row['address_user'];
$subArray['profile_photo'] = $row['profile_photo'];
$subArray['cover_photo'] = $row['cover_photo'];
$subArray['agentarea'] = $row['agentarea'];
$subArray['offertext'] = $row['offertext'];
$subArray['websiteurl'] = $row['websiteurl'];
$subArray['membertype'] = $row['membertype'];
$newarray[] = $subArray;
}
echo(json_encode($newarray,JSON_UNESCAPED_UNICODE));
mysqli_close();
Things I changed:
IN
to shorten the list and to make it clearer. $output
$subArray
Style notes:
One last note: it looks like all that code does little more than make a new copy of $row
. Consider replacing it all with $newarray[] = $row;
and handing the meta_key and meta_value on the javascript side.
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.