0

I'm obviously over-processing the rows being returned from my query, but I don't really understand PHP resources and row counts and arrays.

This is my database call:

$result=mysql_query($query);
if(mysql_num_rows($result)>0){
    header('Content-type: application/json');
    $rows = array();
    while($r = mysql_fetch_assoc($result)){
        $rows[] = array($r);
    }
    echo json_encode($rows);            
};

This is what I'm getting back in my Javascript:

Object {data: Array[23], status: 200, config: Object, statusText: "OK"}
    data: Array[23]
        0:Array[1] <-- superfluous
            0: Object
                Name: "foo"
                Order: "0"
                uID: "1"
        1:Array[1] <-- superfluous
            0: Object
                Name: "bar"
                Order: "1"
                uID: "2"
        ...

There's a superfluous array layer in there. It should look like this:

Object {data: Array[23], status: 200, config: Object, statusText: "OK"}
    data: Array[23]
        0:Object
            Name: "foo"
            Order: "0"
            uID: "1"
        1: Object
            Name: "bar"
            Order: "1"
            uID: "2"
        ...

I'm tempted to remove the rows = array() but I don't know how. I don't get how to turn my db rows into an array properly.

DaveC426913
  • 2,012
  • 6
  • 35
  • 63
  • $rows[] = (array)$r; – Johann Bauer May 27 '16 at 17:10
  • 2
    The problem is at `$rows[] = array($r);` make this like `$rows[] = $r` – Murad Hasan May 27 '16 at 17:10
  • 3
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 27 '16 at 17:12

2 Answers2

1

The problem is at $rows[] = array($r);

Make a little change do this like: $rows[] = $r, I think after that you will get the array not object. You can encode the array with the additional JSON_FORCE_OBJECT so that your array make an object json.

Example after normal json_encode:

Object {data: Array[23], status: 200, config: Object, statusText: "OK"}
    data: Array[23]
        0:Array[3]
            Name: "foo"
            Order: "0"
            uID: "1"
        1: Array[3]
            Name: "bar"
            Order: "1"
            uID: "2"
        ...
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
0

First of all correct

 $rows[] =  $r;

And then in javascript use JSON.parse

JSON.parse(str);
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
  • Thank you. The first fix worked. I'm not sure what the second fix is for. Surely the $http call is expecting and receiving JSON as the response. I mean ... it works without that fix. – DaveC426913 May 27 '16 at 18:59