将MySQL转换为Json ResultSet

I'm trying to turn my results into a json encoded string. I know that now working on an external api seems a bit much, but I do think that it's something that will come in handy as times goes on. Currently, I use the following function:

//API Details
public function APIReturnMembers() {
    $query = <<<SQL
        SELECT uname
            FROM {$this->tprefix}accounts
SQL;
    $encode = array();

    $resource = $this->db->db->prepare( $query );
    $resource->execute();
    foreach($resource as $row) {
        $encode[] = $row;
    }
    echo json_encode($encode);  
} 

It does what it's supposed to as far as returning results go, e.g.:

[{"uname" : "guildemporium"}, {"uname" : "doxramos"}]

When I saw that I was ecstatic! I was on my way to implementing my own API that others could use later on as I actually got somewhere! Now of course I have hit a hickup. Testing my API. To run the code to get the results I used

$api_member_roster = "https://guildemporium.net/api.php?query=members";
$file_contents = @file_get_contents($api_member_roster); // omit warnings
$memberRoster = json_decode($file_contents, true);
print_r($memberRoster);

The good news! It works. I get a result back, yay!

Now the Bad. My Result is

[
    0 => ['uname' => 'guildemporium'],
    1 => ['uname' => 'doxramos']
]

So I've got this nice little number integer interrupting my return so that I can't use my original idea

foreach($memberRoster as $member) {
        echo $member->uname;
}

Where did this extra number come from? Has it come in to ruin my life or am I messing up with my first time playing with the idea of returning results to another member? Find out next time on the X-Files. Or if you already know the answer that'd be great too!

The numbered rows in the array as the array indices of the result set. If you use print_r($encode);exit; right before you use echo json_encode($encode); in your script, you should see the exact same output.

When you create a PHP array, by default all indices are numbered indexes starting from zero and incrementing. You can have mixed array indices of numbers and letters, although is it better (mostly for your own sanity) if you stick to using only numbered indices or natural case English indices, where you would use an array like you would an object, eg.

$arr = [
    'foo' => 'bar',
    'bar' => 'foo'
];

print_r($arr);
/*Array
(
    [foo] => bar
    [bar] => foo
)*/

$arr = [
    'foo','bar'
];

/*Array
(
    [0] => foo
    [1] => bar
)*/

Notice the difference in output? As for your last question; no. This is normal and expected behaviour. Consumers will iterate over the array, most likely using foreach in PHP, or for (x in y) in other languages.

What you're doing is:

$arr = [
    ['abc','123']
];

Which gives you:

Array
(
    [0] => Array
        (
            [0] => abc
            [1] => 123
        )
)

In order to use $member->foo($bar); you need to unserialize the json objects.

In you api itself, you can return the response in json object

In your api.php

function Execute($data){
    // Db  Connectivity
    // query
    $result = mysqli_query($db, $query);

    if( mysqli_num_rows($result) > 0 ){
        $response = ProcessDbData($result);
    }else{
        $response = array();
    }
    mysqli_free_result($result);
    return $response;
}

function ProcessDbData($obj){
    $result = array();
    if(!empty($obj)){
        while($row = mysqli_fetch_assoc($obj)){
            $result[] = $row;
        }
        return $result;
    }
    return $result;
}

function Convert2Json($obj){
    if(is_array($obj)){
        return json_encode($obj);
    }
    return $obj;
}

Calling api.php

$result = $this->ExecuteCurl('api.php?query=members');
print_r($result);

here you $result will contain the json object