1

I am currently working on a search function for my site and I returning the results in JSON. However, some values are returning after returning the first results like the following:

{"fullname":"test name","occupation":"test","industry":"testing","bio":"i am testing stuff.","gender":"f","website":"http:\/\/yhisisasite.com","skills":["writing","reading","math","coding","baseball"],"interests":["coding","sampling","googling","typing","playing"]},{"fullname":null,"occupation":null,"industry":null,"bio":null,"gender":null,"website":null,"skills":["coding","docotrs","soeku","spelling"],"interests":["testing","wintro","skating","hockey","code"]}

I currently have a class to work as a template for the results which looks like this:

class SearchResultUserProfile {
    public $fullname = "";
    public $occupation = "";
    public $industry = "";
    public $bio = "";
    public $gender = "";
    public $website = "";
    public $skills = array();
    public $interests = array();

}

Then to populate those fields I have a few loops during the mysqli fetch:

while ($row = mysqli_fetch_array($result))
{   
    $max = sizeof($user_id_array);
    for($i = 0; $i < $max; $i++)
    {
    //create a new instance or object
    $searchResultUserProfile = new SearchResultUserProfile();
    $searchResultUserProfile->fullname = $row['fullname'];
    $searchResultUserProfile->occupation = $row['occupation'];
    $searchResultUserProfile->industry = $row['industry'];
    $searchResultUserProfile->bio = $row['bio'];
    $searchResultUserProfile->gender = $row['gender'];
    $searchResultUserProfile->website = $row['website'];

    //grab the interests and skills
    $skillDetails = fetchAllUserSkills($user_id_array[$i]);
    foreach($skillDetails as $row) {
        $thistest = $row['skills'];
        array_push($searchResultUserProfile->skills, $thistest);
    }

    $interestDetails = fetchAllUserInterests($user_id_array[$i]);
    foreach($interestDetails as $row) {
        $thistests = $row['interests'];
        array_push($searchResultUserProfile->interests, $thistests);
    }
    array_push($results, $searchResultUserProfile);

    }
    echo json_encode($results);
}

Any idea why this is happening? Is it how I am iterating through the loop or the set up? I am sure I am overlooking something simple but I cannot figure out what it is.

2
  • are you sure $results is not null before json_encode ? Commented Oct 6, 2014 at 17:40
  • Not related, but to generate valid json, you would need to put your last statement outside of the while loop (in case there is more than 1 result). Commented Oct 6, 2014 at 17:55

1 Answer 1

1

The problem is that you are overwriting your $row variable in the inner loops:

while ($row = mysqli_fetch_array($result))
       ^^^^ this is a result row from your query
{   
    $max = sizeof($user_id_array);
    for($i = 0; $i < $max; $i++)
    {
        $searchResultUserProfile = new SearchResultUserProfile();
        $searchResultUserProfile->fullname = $row['fullname'];

        ...

        foreach($skillDetails as $row) {
                                 ^^^^ here you are overwriting your query result
           ...
        }

        ...

    }
    echo json_encode($results);
}

So if $max is more than 1, from the second iteration on you will be using the last result of your last inner loop. And that will not be the result from the query that you expect it to be.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.